some refactoring

This commit is contained in:
Eduardo Bart 2012-06-21 20:58:18 -03:00
parent a7c17cec2e
commit c5674d10ba
23 changed files with 278 additions and 367 deletions

View File

@ -2,7 +2,7 @@ Settings = {}
Settings.exists = g_configs.exists Settings.exists = g_configs.exists
Settings.setNode = g_configs.setNode Settings.setNode = g_configs.setNode
Settings.addNode = g_configs.addNode Settings.mergeNode = g_configs.mergeNode
Settings.getNode = g_configs.getNode Settings.getNode = g_configs.getNode
Settings.remove = g_configs.remove Settings.remove = g_configs.remove
Settings.setList = g_configs.setList Settings.setList = g_configs.setList

View File

@ -173,6 +173,8 @@ SET(framework_SOURCES ${framework_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/core/configmanager.cpp ${CMAKE_CURRENT_LIST_DIR}/core/configmanager.cpp
${CMAKE_CURRENT_LIST_DIR}/core/configmanager.h ${CMAKE_CURRENT_LIST_DIR}/core/configmanager.h
${CMAKE_CURRENT_LIST_DIR}/core/declarations.h ${CMAKE_CURRENT_LIST_DIR}/core/declarations.h
${CMAKE_CURRENT_LIST_DIR}/core/event.h
${CMAKE_CURRENT_LIST_DIR}/core/event.cpp
${CMAKE_CURRENT_LIST_DIR}/core/eventdispatcher.cpp ${CMAKE_CURRENT_LIST_DIR}/core/eventdispatcher.cpp
${CMAKE_CURRENT_LIST_DIR}/core/eventdispatcher.h ${CMAKE_CURRENT_LIST_DIR}/core/eventdispatcher.h
${CMAKE_CURRENT_LIST_DIR}/core/filestream.cpp ${CMAKE_CURRENT_LIST_DIR}/core/filestream.cpp
@ -186,6 +188,8 @@ SET(framework_SOURCES ${framework_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/core/modulemanager.h ${CMAKE_CURRENT_LIST_DIR}/core/modulemanager.h
${CMAKE_CURRENT_LIST_DIR}/core/resourcemanager.cpp ${CMAKE_CURRENT_LIST_DIR}/core/resourcemanager.cpp
${CMAKE_CURRENT_LIST_DIR}/core/resourcemanager.h ${CMAKE_CURRENT_LIST_DIR}/core/resourcemanager.h
${CMAKE_CURRENT_LIST_DIR}/core/scheduledevent.h
${CMAKE_CURRENT_LIST_DIR}/core/scheduledevent.cpp
${CMAKE_CURRENT_LIST_DIR}/core/timer.cpp ${CMAKE_CURRENT_LIST_DIR}/core/timer.cpp
${CMAKE_CURRENT_LIST_DIR}/core/timer.h ${CMAKE_CURRENT_LIST_DIR}/core/timer.h

View File

@ -45,7 +45,7 @@ bool ConfigManager::load(const std::string& file)
m_confsDoc = confsDoc; m_confsDoc = confsDoc;
return true; return true;
} catch(stdext::exception& e) { } catch(stdext::exception& e) {
g_logger.error(stdext::format("Unable to parse configuration file '%s'", e.what())); g_logger.error(stdext::format("Unable to parse configuration file '%s': ", e.what()));
return false; return false;
} }
} }
@ -121,10 +121,10 @@ void ConfigManager::remove(const std::string& key)
void ConfigManager::setNode(const std::string& key, const OTMLNodePtr& node) void ConfigManager::setNode(const std::string& key, const OTMLNodePtr& node)
{ {
remove(key); remove(key);
addNode(key, node); mergeNode(key, node);
} }
void ConfigManager::addNode(const std::string& key, const OTMLNodePtr& node) void ConfigManager::mergeNode(const std::string& key, const OTMLNodePtr& node)
{ {
OTMLNodePtr clone = node->clone(); OTMLNodePtr clone = node->clone();
node->setTag(key); node->setTag(key);

View File

@ -40,7 +40,7 @@ public:
std::vector<std::string> getList(const std::string& key); std::vector<std::string> getList(const std::string& key);
void setNode(const std::string& key, const OTMLNodePtr& node); void setNode(const std::string& key, const OTMLNodePtr& node);
void addNode(const std::string& key, const OTMLNodePtr& node); void mergeNode(const std::string& key, const OTMLNodePtr& node);
OTMLNodePtr getNode(const std::string& key); OTMLNodePtr getNode(const std::string& key);
bool exists(const std::string& key); bool exists(const std::string& key);

View File

@ -27,6 +27,7 @@
EventDispatcher g_eventDispatcher; EventDispatcher g_eventDispatcher;
void EventDispatcher::flush() void EventDispatcher::flush()
{ {
while(!m_eventList.empty()) while(!m_eventList.empty())

View File

@ -23,92 +23,8 @@
#ifndef EVENTDISPATCHER_H #ifndef EVENTDISPATCHER_H
#define EVENTDISPATCHER_H #define EVENTDISPATCHER_H
#include "declarations.h"
#include "clock.h" #include "clock.h"
#include <framework/luascript/luaobject.h> #include "scheduledevent.h"
class Event : public LuaObject
{
public:
Event(const std::function<void()>& callback) : m_callback(callback), m_canceled(false), m_executed(false) { }
virtual ~Event() {
// assure that we lost callback refs
//assert(m_callback == nullptr);
}
virtual void execute() {
if(!m_canceled && !m_executed && m_callback) {
m_callback();
m_executed = true;
}
// reset callback to free object refs
m_callback = nullptr;
}
void cancel() {
m_canceled = true;
m_callback = nullptr;
}
bool isCanceled() { return m_canceled; }
bool isExecuted() { return m_executed; }
protected:
std::function<void()> m_callback;
bool m_canceled;
bool m_executed;
};
class ScheduledEvent : public Event
{
public:
ScheduledEvent(const std::function<void()>& callback, int delay, int maxCycles) : Event(callback) {
m_ticks = g_clock.millis() + delay;
m_delay = delay;
m_maxCycles = maxCycles;
m_cyclesExecuted = 0;
}
void execute() {
if(!m_canceled && m_callback && (m_maxCycles == 0 || m_cyclesExecuted < m_maxCycles)) {
m_callback();
m_executed = true;
// callback may be used in the next cycle
} else {
// reset callback to free object refs
m_callback = nullptr;
}
m_cyclesExecuted++;
}
bool nextCycle() {
if(m_callback && !m_canceled && (m_maxCycles == 0 || m_cyclesExecuted < m_maxCycles)) {
m_ticks += m_delay;
return true;
}
// reset callback to free object refs
m_callback = nullptr;
return false;
}
int ticks() { return m_ticks; }
int reamaningTicks() { return m_ticks - g_clock.millis(); }
int delay() { return m_delay; }
int cyclesExecuted() { return m_cyclesExecuted; }
int maxCycles() { return m_maxCycles; }
private:
ticks_t m_ticks;
int m_delay;
int m_maxCycles;
int m_cyclesExecuted;
};
struct lessScheduledEvent : std::binary_function<ScheduledEventPtr, ScheduledEventPtr&, bool> {
bool operator()(const ScheduledEventPtr& a, const ScheduledEventPtr& b) {
return b->ticks() < a->ticks();
}
};
class EventDispatcher class EventDispatcher
{ {

View File

@ -21,118 +21,101 @@
*/ */
#include "filestream.h" #include "filestream.h"
#include <framework/application.h>
#include <physfs.h> #include <physfs.h>
FileStream::FileStream(const std::string& name, PHYSFS_File *fileHandle) FileStream::FileStream(const std::string& name, PHYSFS_File *fileHandle)
{ {
m_name = name; m_name = name;
m_pos = 0;
m_fileHandle = fileHandle; m_fileHandle = fileHandle;
m_cacheReadPos = 0;
} }
FileStream::~FileStream() FileStream::~FileStream()
{ {
assert(!g_app.isTermianted());
close(); close();
} }
void FileStream::cache() void FileStream::cache()
{ {
if(!m_fileHandle) if(!m_fileHandle)
g_logger.traceError("no file handle to cache"); return;
// cache entire file into cache buffer // cache entire file into data buffer
m_cacheReadPos = PHYSFS_tell(m_fileHandle); m_pos = PHYSFS_tell(m_fileHandle);
PHYSFS_seek(m_fileHandle, 0); PHYSFS_seek(m_fileHandle, 0);
int size = PHYSFS_fileLength(m_fileHandle); int size = PHYSFS_fileLength(m_fileHandle);
m_cacheBuffer.resize(size); m_data.resize(size);
int res = PHYSFS_read(m_fileHandle, &m_cacheBuffer[0], size, 1); int res = PHYSFS_read(m_fileHandle, &m_data[0], size, 1);
if(res == -1) if(res == -1)
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError())); throwError("unable to read file data");
PHYSFS_close(m_fileHandle); close();
m_fileHandle = nullptr;
} }
bool FileStream::close() void FileStream::close()
{ {
if(m_fileHandle) { if(m_fileHandle) {
if(PHYSFS_isInit() && PHYSFS_close(m_fileHandle) == 0) if(!PHYSFS_close(m_fileHandle))
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError())); throwError("close failed");
m_fileHandle = nullptr; m_fileHandle = nullptr;
return true;
} else { } else {
m_cacheBuffer.clear(); m_data.clear();
m_cacheReadPos = 0; m_pos = 0;
return true;
} }
} }
bool FileStream::flush() void FileStream::flush()
{ {
if(!m_fileHandle) if(m_fileHandle && PHYSFS_flush(m_fileHandle) == 0)
return false; throwError("flush failed");
if(PHYSFS_flush(m_fileHandle) == 0) {
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
return false;
}
return true;
} }
int FileStream::read(void *buffer, int size, int nmemb) int FileStream::read(void *buffer, uint32 size, uint32 nmemb)
{ {
if(m_fileHandle) { if(m_fileHandle) {
int res = PHYSFS_read(m_fileHandle, buffer, size, nmemb); int res = PHYSFS_read(m_fileHandle, buffer, size, nmemb);
if(res == -1) { if(res == -1)
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError())); throwError("read failed");
return 0;
}
return res; return res;
} else { } else {
uint maxReadPos = m_cacheBuffer.size()-1; uint maxReadPos = m_data.size()-1;
int writePos = 0; int writePos = 0;
uint8 *outBuffer = (uint8*)buffer; uint8 *outBuffer = (uint8*)buffer;
for(int i=0;i<nmemb;++i) { for(uint i=0;i<nmemb;++i) {
if(m_cacheReadPos+size > maxReadPos) if(m_pos+size > maxReadPos)
return i; return i;
for(int j=0;j<size;++j) for(uint j=0;j<size;++j)
outBuffer[writePos++] = m_cacheBuffer[m_cacheReadPos++]; outBuffer[writePos++] = m_data[m_pos++];
} }
return nmemb; return nmemb;
} }
} }
bool FileStream::write(void *buffer, int count) void FileStream::write(void *buffer, uint32 count)
{ {
if(!m_fileHandle) if(PHYSFS_write(m_fileHandle, buffer, 1, count) != count)
return false; throwError("write failed");
if(PHYSFS_write(m_fileHandle, buffer, 1, count) != count) {
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
return false;
}
return true;
} }
bool FileStream::seek(int pos) void FileStream::seek(uint32 pos)
{ {
if(m_fileHandle) { if(m_fileHandle) {
if(PHYSFS_seek(m_fileHandle, pos) == 0) { if(!PHYSFS_seek(m_fileHandle, pos))
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError())); throwError("seek failed");
return false;
}
} else { } else {
if(pos > (int)m_cacheBuffer.size() || pos < 0) { if(pos > m_data.size())
g_logger.traceError(stdext::format("operation failed on '%s': seek pos cannot be greater than file length", m_name)); throwError("seek failed");
return false; m_pos = pos;
} }
m_cacheReadPos = pos; }
}
return true; void FileStream::skip(uint len)
{
seek(tell() + len);
} }
int FileStream::size() int FileStream::size()
@ -140,7 +123,7 @@ int FileStream::size()
if(m_fileHandle) if(m_fileHandle)
return PHYSFS_fileLength(m_fileHandle); return PHYSFS_fileLength(m_fileHandle);
else else
return m_cacheBuffer.size(); return m_data.size();
} }
int FileStream::tell() int FileStream::tell()
@ -148,7 +131,7 @@ int FileStream::tell()
if(m_fileHandle) if(m_fileHandle)
return PHYSFS_tell(m_fileHandle); return PHYSFS_tell(m_fileHandle);
else else
return m_cacheReadPos; return m_pos;
} }
uint8 FileStream::getU8() uint8 FileStream::getU8()
@ -156,15 +139,13 @@ uint8 FileStream::getU8()
uint8 v = 0; uint8 v = 0;
if(m_fileHandle) { if(m_fileHandle) {
if(PHYSFS_read(m_fileHandle, &v, 1, 1) != 1) if(PHYSFS_read(m_fileHandle, &v, 1, 1) != 1)
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError())); throwError("read failed");
} else { } else {
if(m_cacheReadPos+1 > m_cacheBuffer.size()) { if(m_pos+1 > m_data.size())
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name)); throwError("read failed");
return 0;
}
v = m_cacheBuffer[m_cacheReadPos]; v = m_data[m_pos];
m_cacheReadPos += 1; m_pos += 1;
} }
return v; return v;
} }
@ -174,15 +155,13 @@ uint16 FileStream::getU16()
uint16 v = 0; uint16 v = 0;
if(m_fileHandle) { if(m_fileHandle) {
if(PHYSFS_readULE16(m_fileHandle, &v) == 0) if(PHYSFS_readULE16(m_fileHandle, &v) == 0)
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError())); throwError("read failed");
} else { } else {
if(m_cacheReadPos+2 > m_cacheBuffer.size()) { if(m_pos+2 > m_data.size())
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name)); throwError("read failed");
return 0;
}
v = stdext::readLE16(&m_cacheBuffer[m_cacheReadPos]); v = stdext::readLE16(&m_data[m_pos]);
m_cacheReadPos += 2; m_pos += 2;
} }
return v; return v;
} }
@ -192,15 +171,13 @@ uint32 FileStream::getU32()
uint32 v = 0; uint32 v = 0;
if(m_fileHandle) { if(m_fileHandle) {
if(PHYSFS_readULE32(m_fileHandle, &v) == 0) if(PHYSFS_readULE32(m_fileHandle, &v) == 0)
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError())); throwError("read failed");
} else { } else {
if(m_cacheReadPos+4 > m_cacheBuffer.size()) { if(m_pos+4 > m_data.size())
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name)); throwError("read failed");
return 0;
}
v = stdext::readLE32(&m_cacheBuffer[m_cacheReadPos]); v = stdext::readLE32(&m_data[m_pos]);
m_cacheReadPos += 4; m_pos += 4;
} }
return v; return v;
} }
@ -210,15 +187,12 @@ uint64 FileStream::getU64()
uint64 v = 0; uint64 v = 0;
if(m_fileHandle) { if(m_fileHandle) {
if(PHYSFS_readULE64(m_fileHandle, (PHYSFS_uint64*)&v) == 0) if(PHYSFS_readULE64(m_fileHandle, (PHYSFS_uint64*)&v) == 0)
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError())); throwError("read failed");
} else { } else {
if(m_cacheReadPos+8 > m_cacheBuffer.size()) { if(m_pos+8 > m_data.size())
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name)); throwError("read failed");
return 0; v = stdext::readLE64(&m_data[m_pos]);
} m_pos += 8;
v = stdext::readLE64(&m_cacheBuffer[m_cacheReadPos]);
m_cacheReadPos += 8;
} }
return v; return v;
} }
@ -235,66 +209,47 @@ std::string FileStream::getString()
else else
str = std::string(buffer, len); str = std::string(buffer, len);
} else { } else {
if(m_cacheReadPos+len > m_cacheBuffer.size()) { if(m_pos+len > m_data.size()) {
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name)); g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name));
return 0; return 0;
} }
str = std::string((char*)&m_cacheBuffer[m_cacheReadPos], len); str = std::string((char*)&m_data[m_pos], len);
m_cacheReadPos += len; m_pos += len;
} }
} else { } else {
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
} }
return str; return str;
} }
uint8 FileStream::readFirstNode(uint32& type)
{
dump << "first";
uint8 node = getU8();
if(node != NODE_START)
stdext::throw_exception("failed to read first node");
type = getU32();
return node;
}
uint8 FileStream::readNextNode(uint8 oldNode, uint32& type)
{
dump << "next";
// end of old node
if(getU8() != NODE_END)
stdext::throw_exception("node not ended");
// next node
uint8 node = getU8();
if(oldNode != NODE_START)
stdext::throw_exception("invalid node start");
type = getU32();
return node;
}
void FileStream::addU8(uint8 v) void FileStream::addU8(uint8 v)
{ {
if(PHYSFS_write(m_fileHandle, &v, 1, 1) != 1) if(PHYSFS_write(m_fileHandle, &v, 1, 1) != 1)
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError())); throwError("write failed");
} }
void FileStream::addU16(uint8 v) void FileStream::addU16(uint8 v)
{ {
if(PHYSFS_writeULE16(m_fileHandle, v) == 0) if(PHYSFS_writeULE16(m_fileHandle, v) == 0)
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError())); throwError("write failed");
} }
void FileStream::addU32(uint8 v) void FileStream::addU32(uint8 v)
{ {
if(PHYSFS_writeULE32(m_fileHandle, v) == 0) if(PHYSFS_writeULE32(m_fileHandle, v) == 0)
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError())); throwError("write failed");
} }
void FileStream::addU64(uint8 v) void FileStream::addU64(uint8 v)
{ {
if(PHYSFS_writeULE64(m_fileHandle, v) == 0) if(PHYSFS_writeULE64(m_fileHandle, v) == 0)
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError())); throwError("write failed");
}
void FileStream::throwError(const std::string& message)
{
std::string completeMessage = stdext::format("in file '%s': %s", m_name, message);
if(m_fileHandle)
completeMessage += std::string(": ") + PHYSFS_getLastError();
stdext::throw_exception(completeMessage);
} }

View File

@ -29,26 +29,17 @@ struct PHYSFS_File;
class FileStream class FileStream
{ {
enum {
NODE_START = 0xFE,
NODE_END = 0xFF,
ESCAPE_CHAR = 0xFD,
};
protected:
FileStream(const std::string& name, PHYSFS_File *fileHandle);
friend class ResourceManager;
public: public:
FileStream(const std::string& name, PHYSFS_File *fileHandle);
~FileStream(); ~FileStream();
void cache(); void cache();
bool close(); void close();
bool flush(); void flush();
bool write(void *buffer, int count); void write(void *buffer, uint count);
int read(void *buffer, int size, int nmemb = 1); int read(void *buffer, uint size, uint nmemb = 1);
bool seek(int pos); void seek(uint pos);
void skip(uint len);
int size(); int size();
int tell(); int tell();
std::string name() { return m_name; } std::string name() { return m_name; }
@ -60,20 +51,21 @@ public:
uint32 getU32(); uint32 getU32();
uint64 getU64(); uint64 getU64();
std::string getString(); std::string getString();
void addU8(uint8 v); void addU8(uint8 v);
void addU16(uint8 v); void addU16(uint8 v);
void addU32(uint8 v); void addU32(uint8 v);
void addU64(uint8 v); void addU64(uint8 v);
uint8 readFirstNode(uint32& type);
uint8 readNextNode(uint8 oldNode, uint32& type);
private: private:
void checkWrite();
void throwError(const std::string& message);
std::string m_name; std::string m_name;
PHYSFS_File *m_fileHandle; PHYSFS_File *m_fileHandle;
std::vector<uint8_t> m_cacheBuffer; std::vector<uint8_t> m_data;
uint m_cacheReadPos; uint m_pos;
}; };
#endif #endif

View File

@ -521,7 +521,7 @@ void Application::registerLuaFunctions()
g_lua.bindSingletonFunction("g_configs", "exists", &ConfigManager::exists, &g_configs); g_lua.bindSingletonFunction("g_configs", "exists", &ConfigManager::exists, &g_configs);
g_lua.bindSingletonFunction("g_configs", "remove", &ConfigManager::remove, &g_configs); g_lua.bindSingletonFunction("g_configs", "remove", &ConfigManager::remove, &g_configs);
g_lua.bindSingletonFunction("g_configs", "setNode", &ConfigManager::setNode, &g_configs); g_lua.bindSingletonFunction("g_configs", "setNode", &ConfigManager::setNode, &g_configs);
g_lua.bindSingletonFunction("g_configs", "addNode", &ConfigManager::addNode, &g_configs); g_lua.bindSingletonFunction("g_configs", "mergeNode", &ConfigManager::mergeNode, &g_configs);
g_lua.bindSingletonFunction("g_configs", "getNode", &ConfigManager::getNode, &g_configs); g_lua.bindSingletonFunction("g_configs", "getNode", &ConfigManager::getNode, &g_configs);
// PlatformWindow // PlatformWindow

View File

@ -87,17 +87,14 @@ int OggSoundFile::cb_seek(void* source, ogg_int64_t offset, int whence)
FileStream *file = static_cast<FileStream*>(source); FileStream *file = static_cast<FileStream*>(source);
switch(whence) { switch(whence) {
case SEEK_SET: case SEEK_SET:
if(file->seek(offset)) file->seek(offset);
return 0; return 0;
break;
case SEEK_CUR: case SEEK_CUR:
if(file->seek(file->tell() + offset)) file->seek(file->tell() + offset);
return 0; return 0;
break;
case SEEK_END: case SEEK_END:
if(file->seek(file->size() + offset)) file->seek(file->size() + offset);
return 0; return 0;
break;
} }
return -1; return -1;
} }

View File

@ -46,7 +46,7 @@ bool SoundBuffer::fillBuffer(const SoundFilePtr& soundFile)
DataBuffer<char> samples(soundFile->getSize()); DataBuffer<char> samples(soundFile->getSize());
int read = soundFile->read(&samples[0], soundFile->getSize()); int read = soundFile->read(&samples[0], soundFile->getSize());
if(read <= 0) { if(read == 0) {
g_logger.error(stdext::format("unable to fill audio buffer data for '%s'", soundFile->getName())); g_logger.error(stdext::format("unable to fill audio buffer data for '%s'", soundFile->getName()));
return false; return false;
} }

View File

@ -28,7 +28,7 @@
#include <framework/core/timer.h> #include <framework/core/timer.h>
#include <framework/graphics/cachedtext.h> #include <framework/graphics/cachedtext.h>
class AnimatedText : public Thing class AnimatedText
{ {
public: public:
AnimatedText(); AnimatedText();

View File

@ -37,6 +37,7 @@ Item::Item() : Thing()
m_id = 0; m_id = 0;
m_countOrSubType = 1; m_countOrSubType = 1;
m_shaderProgram = g_shaders.getDefaultItemShader(); m_shaderProgram = g_shaders.getDefaultItemShader();
m_otbType = g_things.getNullOtbType();
} }
ItemPtr Item::create(int id) ItemPtr Item::create(int id)
@ -46,6 +47,13 @@ ItemPtr Item::create(int id)
return item; return item;
} }
ItemPtr Item::createFromOtb(int id)
{
ItemPtr item = ItemPtr(new Item);
item->setOtbId(id);
return item;
}
void Item::draw(const Point& dest, float scaleFactor, bool animate) void Item::draw(const Point& dest, float scaleFactor, bool animate)
{ {
if(m_id == 0) if(m_id == 0)
@ -177,7 +185,15 @@ void Item::draw(const Point& dest, float scaleFactor, bool animate)
void Item::setId(uint32 id) void Item::setId(uint32 id)
{ {
m_datType = g_things.getDatType(id, DatItemCategory); m_datType = g_things.getDatType(id, DatItemCategory);
m_id = id; m_otbType = g_things.findOtbForClientId(id);
m_id = m_datType->getId();
}
void Item::setOtbId(uint16 id)
{
m_otbType = g_things.getOtbType(id);
m_datType = g_things.getDatType(m_otbType->getClientId(), DatItemCategory);
m_id = m_datType->getId();
} }
bool Item::unserializeAttr(FileStreamPtr fin) bool Item::unserializeAttr(FileStreamPtr fin)

View File

@ -25,6 +25,7 @@
#include <framework/global.h> #include <framework/global.h>
#include "thing.h" #include "thing.h"
#include "thingtypeotb.h"
enum AttrTypes_t { enum AttrTypes_t {
ATTR_END = 0, ATTR_END = 0,
@ -73,10 +74,12 @@ public:
Item(); Item();
static ItemPtr create(int id); static ItemPtr create(int id);
static ItemPtr createFromOtb(int id);
void draw(const Point& dest, float scaleFactor, bool animate); void draw(const Point& dest, float scaleFactor, bool animate);
void setId(uint32 id); void setId(uint32 id);
void setOtbId(uint16 id);
void setCountOrSubType(int value) { m_countOrSubType = value; } void setCountOrSubType(int value) { m_countOrSubType = value; }
void setCount(int count) { m_countOrSubType = count; } void setCount(int count) { m_countOrSubType = count; }
void setSubType(int subType) { m_countOrSubType = subType; } void setSubType(int subType) { m_countOrSubType = subType; }
@ -108,6 +111,7 @@ private:
uint32 m_actionId, m_uniqueId; uint32 m_actionId, m_uniqueId;
std::string m_name, m_text, m_description; std::string m_name, m_text, m_description;
PainterShaderProgramPtr m_shaderProgram; PainterShaderProgramPtr m_shaderProgram;
ThingTypeOtbPtr m_otbType;
}; };
#endif #endif

View File

@ -60,6 +60,7 @@ void Map::notificateTileUpdateToMapViews(const Position& pos)
bool Map::loadOtbm(const std::string& fileName) bool Map::loadOtbm(const std::string& fileName)
{ {
#if 0
FileStreamPtr fin = g_resources.openFile(fileName); FileStreamPtr fin = g_resources.openFile(fileName);
if (!fin) { if (!fin) {
g_logger.error(stdext::format("Unable to load map '%s'", fileName)); g_logger.error(stdext::format("Unable to load map '%s'", fileName));
@ -72,7 +73,8 @@ bool Map::loadOtbm(const std::string& fileName)
} }
uint32 type = 0; uint32 type = 0;
uint8 root = fin->readFirstNode(type); uint8 root = 0;
root = fin->readNode(root, type);
uint32 headerVersion = fin->getU32(); uint32 headerVersion = fin->getU32();
if (!headerVersion || headerVersion > 3) { if (!headerVersion || headerVersion > 3) {
@ -95,7 +97,7 @@ bool Map::loadOtbm(const std::string& fileName)
if (headerMinorItems > g_things.getOtbMinorVersion()) if (headerMinorItems > g_things.getOtbMinorVersion())
g_logger.warning("This map needs an updated OTB."); g_logger.warning("This map needs an updated OTB.");
uint8 node = fin->readNextNode(root, type); uint8 node = fin->readNode(root, type);
if (type != OTBM_MAP_DATA) { if (type != OTBM_MAP_DATA) {
g_logger.error("Could not read data node."); g_logger.error("Could not read data node.");
return false; return false;
@ -128,7 +130,7 @@ bool Map::loadOtbm(const std::string& fileName)
uint8 nodeMapData; uint8 nodeMapData;
do { do {
nodeMapData = fin->readNextNode(node, type); nodeMapData = fin->readNode(node, type);
if (type == OTBM_TILE_AREA) { if (type == OTBM_TILE_AREA) {
uint16 baseX = fin->getU16(), baseY = fin->getU16(); uint16 baseX = fin->getU16(), baseY = fin->getU16();
@ -136,7 +138,7 @@ bool Map::loadOtbm(const std::string& fileName)
uint8 nodeTile; uint8 nodeTile;
do { do {
nodeTile = fin->readNextNode(nodeMapData, type); nodeTile = fin->readNode(nodeMapData, type);
if (type == OTBM_TILE || type == OTBM_HOUSETILE) { if (type == OTBM_TILE || type == OTBM_HOUSETILE) {
TilePtr tile = 0; TilePtr tile = 0;
@ -174,7 +176,7 @@ bool Map::loadOtbm(const std::string& fileName)
flags |= TILESTATE_REFRESH; flags |= TILESTATE_REFRESH;
break; break;
} case OTBM_ATTR_ITEM: { } case OTBM_ATTR_ITEM: {
ItemPtr item = Item::create(fin->getU16()); ItemPtr item = Item::createFromOtb(fin->getU16());
if (!item) { if (!item) {
g_logger.error(stdext::format("failed to create new item at tile pos %d, %d, %d", px, py, pz)); g_logger.error(stdext::format("failed to create new item at tile pos %d, %d, %d", px, py, pz));
return false; return false;
@ -198,10 +200,10 @@ bool Map::loadOtbm(const std::string& fileName)
uint8 nodeItem; uint8 nodeItem;
do { do {
nodeItem = fin->readNextNode(nodeTile, type); nodeItem = fin->readNode(nodeTile, type);
if (type == OTBM_ITEM) { if (type == OTBM_ITEM) {
ItemPtr item = Item::create(fin->getU16()); ItemPtr item = Item::createFromOtb(fin->getU16());
if (!item) { if (!item) {
g_logger.error(stdext::format("failed to create new item at pos %d, %d, %d", px, py, pz)); g_logger.error(stdext::format("failed to create new item at pos %d, %d, %d", px, py, pz));
return false; return false;
@ -246,7 +248,7 @@ bool Map::loadOtbm(const std::string& fileName)
} else if (type == OTBM_TOWNS) { } else if (type == OTBM_TOWNS) {
uint8 nodeTown; uint8 nodeTown;
do { do {
nodeTown = fin->readNextNode(nodeMapData, type); nodeTown = fin->readNode(nodeMapData, type);
if (type == OTBM_TOWN) { if (type == OTBM_TOWN) {
uint32 townId = fin->getU32(); uint32 townId = fin->getU32();
@ -261,7 +263,7 @@ bool Map::loadOtbm(const std::string& fileName)
} else if (type == OTBM_WAYPOINTS && headerVersion > 1) { } else if (type == OTBM_WAYPOINTS && headerVersion > 1) {
uint8 nodeWaypoint; uint8 nodeWaypoint;
do { do {
nodeWaypoint = fin->readNextNode(nodeMapData, type); nodeWaypoint = fin->readNode(nodeMapData, type);
if (type == OTBM_WAYPOINT) { if (type == OTBM_WAYPOINT) {
std::string name = fin->getString(); std::string name = fin->getString();
@ -276,6 +278,8 @@ bool Map::loadOtbm(const std::string& fileName)
// TODO: Load house & spawns. // TODO: Load house & spawns.
return true; return true;
#endif
return false;
} }
bool Map::loadOtcm(const std::string& fileName) bool Map::loadOtcm(const std::string& fileName)

View File

@ -62,6 +62,7 @@ void SpriteManager::unload()
{ {
m_spritesCount = 0; m_spritesCount = 0;
m_signature = 0; m_signature = 0;
m_spritesFile = nullptr;
} }
ImagePtr SpriteManager::getSpriteImage(int id) ImagePtr SpriteManager::getSpriteImage(int id)

View File

@ -23,12 +23,12 @@
#ifndef STATICTEXT_H #ifndef STATICTEXT_H
#define STATICTEXT_H #define STATICTEXT_H
#include "thing.h"
#include <framework/graphics/cachedtext.h> #include <framework/graphics/cachedtext.h>
#include <framework/graphics/fontmanager.h> #include <framework/graphics/fontmanager.h>
#include <framework/core/timer.h> #include <framework/core/timer.h>
#include <framework/luascript/luaobject.h>
class StaticText : public Thing class StaticText : public LuaObject
{ {
public: public:
StaticText(); StaticText();

View File

@ -31,7 +31,6 @@
Thing::Thing() Thing::Thing()
{ {
m_datType = g_things.getNullDatType(); m_datType = g_things.getNullDatType();
m_otbType = g_things.getNullOtbType();
} }
int Thing::getStackPriority() int Thing::getStackPriority()

View File

@ -25,7 +25,6 @@
#include "declarations.h" #include "declarations.h"
#include "thingtypedat.h" #include "thingtypedat.h"
#include "thingtypeotb.h"
#include <framework/luascript/luaobject.h> #include <framework/luascript/luaobject.h>
struct Light struct Light
@ -135,7 +134,6 @@ public:
protected: protected:
Position m_position; Position m_position;
ThingTypeDatPtr m_datType; ThingTypeDatPtr m_datType;
ThingTypeOtbPtr m_otbType;
}; };
#endif #endif

View File

@ -94,6 +94,7 @@ bool ThingTypeManager::loadDat(const std::string& file)
bool ThingTypeManager::loadOtb(const std::string& file) bool ThingTypeManager::loadOtb(const std::string& file)
{ {
/*
try { try {
FileStreamPtr fin = g_resources.openFile(file); FileStreamPtr fin = g_resources.openFile(file);
if (!fin) if (!fin)
@ -102,21 +103,19 @@ bool ThingTypeManager::loadOtb(const std::string& file)
m_otbVersion = fin->getU32(); m_otbVersion = fin->getU32();
uint32 type = 0; uint32 type = 0;
uint8 node = fin->readFirstNode(type); uint8 node = 0;
fin->readNode(node, type);
fin->getU32(); // flags fin->getU32(); // flags
uint8 version = fin->getU8();
if(fin->getU8() == 0x01) { // version
fin->getU8(); // length
m_otbMajorVersion = fin->getU32(); m_otbMajorVersion = fin->getU32();
m_otbMinorVersion = fin->getU32(); m_otbMinorVersion = fin->getU32();
fin->getU32(); // build number fin->getU32(); // build number
} fin->skip(128);
while((node = fin->readNextNode(node, type))) { while(fin->readNode(node, type)) {
ThingTypeOtbPtr otbType(new ThingTypeOtb); ThingTypeOtbPtr otbType(new ThingTypeOtb);
otbType->unserialize((OtbCategory)type, fin); otbType->unserialize(fin);
addOtbType(otbType); addOtbType(otbType);
} }
@ -126,14 +125,17 @@ bool ThingTypeManager::loadOtb(const std::string& file)
g_logger.error(stdext::format("failed to load otb '%s': %s", file, e.what())); g_logger.error(stdext::format("failed to load otb '%s': %s", file, e.what()));
return false; return false;
} }
*/
return false;
} }
bool ThingTypeManager::loadXml(const std::string& file) bool ThingTypeManager::loadXml(const std::string& file)
{ {
/* /*
TiXmlDocument doc(name.c_str()); try {
TiXmlDocument doc(file.c_str());
if (!doc.LoadFile()) { if (!doc.LoadFile()) {
g_logger.error(stdext::format("failed to load xml '%s'", name)); g_logger.error(stdext::format("failed to load xml '%s'", file));
return false; return false;
} }
@ -177,15 +179,15 @@ bool ThingTypeManager::loadXml(const std::string& file)
continue; continue;
} }
ThingTypeOtbPtr iType = getType(id); ThingTypeOtbPtr otbType = getType(id);
if (!iType) { if (!otbType) {
iType = ThingTypeOtbPtr(new ItemData); otbType = ThingTypeOtbPtr(new ItemData);
iType->id = idEx ? idEx : id; otbType->id = idEx ? idEx : id;
iType->name = name; otbType->name = name;
addType(iType->id, iType); addType(otbType->id, otbType);
} }
iType->name = name; otbType->name = name;
for (TiXmlElement *attr = element->FirstChildElement(); attr; attr = attr->NextSiblingElement()) { for (TiXmlElement *attr = element->FirstChildElement(); attr; attr = attr->NextSiblingElement()) {
if (attr->ValueTStr() != "attribute") if (attr->ValueTStr() != "attribute")
@ -195,40 +197,43 @@ bool ThingTypeManager::loadXml(const std::string& file)
std::string value = attr->Attribute("value"); std::string value = attr->Attribute("value");
if (key == "type") { if (key == "type") {
if (value == "magicfield") if (value == "magicfield")
iType->category = IsMagicField; otbType->category = IsMagicField;
else if (value == "key") else if (value == "key")
iType->category = IsKey; otbType->category = IsKey;
else if (value == "depot") else if (value == "depot")
iType->isDepot = true; otbType->isDepot = true;
else if (value == "teleport") else if (value == "teleport")
iType->category = IsTeleport; otbType->category = IsTeleport;
else if (value == "bed") else if (value == "bed")
iType->isBed = true; otbType->isBed = true;
else if (value == "door") else if (value == "door")
iType->category = IsDoor; otbType->category = IsDoor;
} else if (key == "name") { } else if (key == "name") {
iType->name = value; otbType->name = value;
} else if (key == "description") { } else if (key == "description") {
iType->description = value; otbType->description = value;
} else if (key == "weight") { } else if (key == "weight") {
iType->weight = stdext::unsafe_cast<double>(stdext::unsafe_cast<double>(value) / 100.f); otbType->weight = stdext::unsafe_cast<double>(stdext::unsafe_cast<double>(value) / 100.f);
} else if (key == "containerSize") { } else if (key == "containerSize") {
int containerSize = stdext::unsafe_cast<int>(value); int containerSize = stdext::unsafe_cast<int>(value);
if (containerSize) if (containerSize)
iType->containerSize = containerSize; otbType->containerSize = containerSize;
iType->category = IsContainer; otbType->category = IsContainer;
} else if (key == "writeable") { } else if (key == "writeable") {
if (!value.empty()) if (!value.empty())
iType->category = IsWritable; otbType->category = IsWritable;
} else if (key == "maxTextLen") { } else if (key == "maxTextLen") {
iType->maxTextLength = stdext::unsafe_cast<int>(value); otbType->maxTextLength = stdext::unsafe_cast<int>(value);
} else if (key == "charges") { } else if (key == "charges") {
iType->charges = stdext::unsafe_cast<int>(value); otbType->charges = stdext::unsafe_cast<int>(value);
} }
} }
} }
doc.Clear(); doc.Clear();
} catch(stdext::exception& e) {
}
return true; return true;
*/ */
return false; return false;
@ -242,6 +247,15 @@ void ThingTypeManager::addOtbType(const ThingTypeOtbPtr& otbType)
m_otbTypes[id] = otbType; m_otbTypes[id] = otbType;
} }
ThingTypeOtbPtr ThingTypeManager::findOtbForClientId(uint16 id)
{
for(const ThingTypeOtbPtr& otbType : m_otbTypes) {
if(otbType->getClientId() == id)
return otbType;
}
return m_nullOtbType;
}
ThingTypeDatPtr ThingTypeManager::getDatType(uint16 id, DatCategory category) ThingTypeDatPtr ThingTypeManager::getDatType(uint16 id, DatCategory category)
{ {
if(category >= DatLastCategory || id >= m_datTypes[category].size()) { if(category >= DatLastCategory || id >= m_datTypes[category].size()) {

View File

@ -40,6 +40,7 @@ public:
bool loadXml(const std::string& file); bool loadXml(const std::string& file);
void addOtbType(const ThingTypeOtbPtr& otbType); void addOtbType(const ThingTypeOtbPtr& otbType);
ThingTypeOtbPtr findOtbForClientId(uint16 id);
ThingTypeDatPtr& getNullDatType() { return m_nullDatType; } ThingTypeDatPtr& getNullDatType() { return m_nullDatType; }
ThingTypeOtbPtr& getNullOtbType() { return m_nullOtbType; } ThingTypeOtbPtr& getNullOtbType() { return m_nullOtbType; }

View File

@ -28,37 +28,45 @@
ThingTypeOtb::ThingTypeOtb() ThingTypeOtb::ThingTypeOtb()
{ {
m_category = OtbInvalidCateogry; m_category = OtbInvalidCateogry;
m_serverId = 0;
m_clientId = 0;
} }
void ThingTypeOtb::unserialize(OtbCategory category, const FileStreamPtr& fin) void ThingTypeOtb::unserialize(const FileStreamPtr& fin)
{ {
m_null = false; m_null = false;
m_category = category;
uint8 zero = fin->getU8();
assert(zero == 0);
m_category = (OtbCategory)fin->getU8();
fin->getU32(); // skip flags fin->getU32(); // skip flags
bool done = false; bool done = false;
for(int i=0;i<OtbLastAttrib; ++i) { for(int i=0;i<OtbLastAttrib; ++i) {
int attr = fin->getU8(); int attr = fin->getU8();
if(attr == 0) { if(attr == 0) {
done = true; done = true;
break; break;
} else if(attr == 0xFF) {
fin->seek(fin->tell() - 1);
done = true;
break;
} }
uint16 len = fin->getU16(); int len = fin->getU16();
switch(attr) { switch(attr) {
case OtbAttribServerId: case OtbAttribServerId:
m_serverId = fin->getU16(); m_serverId = fin->getU16();
assert(len == 2);
break; break;
case OtbAttribClientId: case OtbAttribClientId:
m_clientId = fin->getU16(); m_clientId = fin->getU16();
break; assert(len == 2);
case OtbAttribSpeed:
fin->getU16(); // skip speed
break; break;
default: default:
fin->seek(len); // skip attribute fin->skip(len); // skip attribute
break; break;
} }
} }

View File

@ -84,7 +84,8 @@ class ThingTypeOtb : public LuaObject
public: public:
ThingTypeOtb(); ThingTypeOtb();
void unserialize(OtbCategory category, const FileStreamPtr& fin); void unserialize(const FileStreamPtr& fin);
void unserializeXML();
uint16 getServerId() { return m_serverId; } uint16 getServerId() { return m_serverId; }
uint16 getClientId() { return m_clientId; } uint16 getClientId() { return m_clientId; }