Init
This commit is contained in:
261
DependentExtensions/cat/io/AsyncBuffer.hpp
Normal file
261
DependentExtensions/cat/io/AsyncBuffer.hpp
Normal file
@ -0,0 +1,261 @@
|
||||
/*
|
||||
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
* Neither the name of LibCat nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef CAT_ASYNC_BUFFER_HPP
|
||||
#define CAT_ASYNC_BUFFER_HPP
|
||||
|
||||
#include <cat/Platform.hpp>
|
||||
#include <cat/port/FastDelegate.h>
|
||||
|
||||
#if defined(CAT_OS_WINDOWS)
|
||||
# include <cat/port/WindowsInclude.hpp>
|
||||
#endif
|
||||
|
||||
namespace cat {
|
||||
|
||||
|
||||
class ThreadPoolLocalStorage;
|
||||
class AsyncBuffer;
|
||||
|
||||
|
||||
typedef fastdelegate::FastDelegate4<ThreadPoolLocalStorage *, int, AsyncBuffer *, u32, bool> AsyncCallback;
|
||||
|
||||
|
||||
// Overlapped base object
|
||||
#if defined(CAT_OS_WINDOWS)
|
||||
typedef OVERLAPPED AsyncOv;
|
||||
#else
|
||||
#error "TODO"
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
AsyncBuffer: Utility object representing the buffers for a single I/O operation.
|
||||
|
||||
This is flexible enough to represent network and file IO buffers.
|
||||
*/
|
||||
class AsyncBuffer
|
||||
{
|
||||
private:
|
||||
AsyncOv _ov;
|
||||
AsyncCallback _callback;
|
||||
u8 *_data;
|
||||
u32 _data_bytes, _tag_bytes;
|
||||
u8 _tag[1];
|
||||
|
||||
public:
|
||||
// Reset AsyncOv and set offset and callback
|
||||
CAT_INLINE void Reset(const AsyncCallback &callback, u64 offset = 0)
|
||||
{
|
||||
#if defined(CAT_OS_WINDOWS)
|
||||
_ov.hEvent = 0;
|
||||
_ov.Internal = 0;
|
||||
_ov.InternalHigh = 0;
|
||||
_ov.OffsetHigh = (u32)(offset >> 32);
|
||||
_ov.Offset = (u32)offset;
|
||||
#else
|
||||
#error "TODO"
|
||||
#endif
|
||||
_callback = callback;
|
||||
}
|
||||
|
||||
public:
|
||||
static CAT_INLINE u32 OVERHEAD() { return (u32)(offsetof(AsyncBuffer, _tag)); }
|
||||
|
||||
CAT_INLINE AsyncOv *GetOv() { return &_ov; }
|
||||
CAT_INLINE u64 GetOffset() { return ((u64)_ov.OffsetHigh << 32) | _ov.Offset; }
|
||||
|
||||
CAT_INLINE bool Call(ThreadPoolLocalStorage *tls, int error, AsyncBuffer *buffer, u32 bytes)
|
||||
{
|
||||
if (!_callback) return true;
|
||||
return _callback(tls, error, buffer, bytes);
|
||||
}
|
||||
|
||||
CAT_INLINE void Zero()
|
||||
{
|
||||
CAT_CLR(_data, _data_bytes);
|
||||
}
|
||||
|
||||
public:
|
||||
CAT_INLINE u32 GetDataBytes() { return _data_bytes; }
|
||||
|
||||
CAT_INLINE u8 *GetData() { return reinterpret_cast<u8*>( _data ); }
|
||||
|
||||
template<class T>
|
||||
CAT_INLINE T *GetData() { return reinterpret_cast<T*>( _data ); }
|
||||
|
||||
template<class T>
|
||||
CAT_INLINE T *GetData(T * &ptr) { return (ptr = reinterpret_cast<T*>( _data )); }
|
||||
|
||||
public:
|
||||
CAT_INLINE u32 GetTagBytes() { return _tag_bytes; }
|
||||
|
||||
CAT_INLINE u8 *GetTagData() { return _tag; }
|
||||
|
||||
template<class T>
|
||||
CAT_INLINE T *GetTag() { return reinterpret_cast<T*>( _tag ); }
|
||||
|
||||
template<class T>
|
||||
CAT_INLINE T *GetTag(T * &ptr) { return (ptr = reinterpret_cast<T*>( _tag )); }
|
||||
|
||||
public:
|
||||
// Acquire with built-in data pointer
|
||||
static CAT_INLINE AsyncBuffer *Acquire(AsyncBuffer * &ptr, u32 data_bytes = 0, u32 tag_bytes = 0)
|
||||
{
|
||||
const u32 OVERHEAD_BYTES = (u32)(offsetof(AsyncBuffer, _tag));
|
||||
|
||||
AsyncBuffer *buffer = reinterpret_cast<AsyncBuffer*>(
|
||||
RegionAllocator::ii->Acquire(OVERHEAD_BYTES + data_bytes + tag_bytes) );
|
||||
|
||||
if (!buffer) return 0;
|
||||
|
||||
buffer->_data_bytes = data_bytes;
|
||||
buffer->_tag_bytes = tag_bytes;
|
||||
buffer->_data = buffer->_tag + tag_bytes;
|
||||
|
||||
return (ptr = buffer);
|
||||
}
|
||||
|
||||
// Change number of data bytes allocated to the buffer
|
||||
// Returns a new data pointer that may be different from the old data pointer
|
||||
CAT_INLINE AsyncBuffer *Resize(u32 data_bytes)
|
||||
{
|
||||
const u32 OVERHEAD_BYTES = (u32)(offsetof(AsyncBuffer, _tag));
|
||||
|
||||
AsyncBuffer *buffer = reinterpret_cast<AsyncBuffer*>(
|
||||
RegionAllocator::ii->Resize( this,
|
||||
OVERHEAD_BYTES + _tag_bytes + data_bytes) );
|
||||
|
||||
if (!buffer) return 0;
|
||||
|
||||
buffer->_data_bytes = data_bytes;
|
||||
buffer->_data = buffer->_tag + buffer->_tag_bytes;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public:
|
||||
// Acquire with built-in data pointer
|
||||
static CAT_INLINE u8 *Acquire(u32 data_bytes = 0, u32 tag_bytes = 0)
|
||||
{
|
||||
AsyncBuffer *buffer;
|
||||
|
||||
if (!Acquire(buffer, data_bytes, tag_bytes)) return 0;
|
||||
|
||||
return buffer->_data;
|
||||
}
|
||||
|
||||
// Acquire with built-in data pointer
|
||||
template<class T>
|
||||
static CAT_INLINE T *Acquire(T * &data, u32 tag_bytes = 0)
|
||||
{
|
||||
AsyncBuffer *buffer;
|
||||
|
||||
if (!Acquire(buffer, sizeof(T), tag_bytes)) return 0;
|
||||
|
||||
return (data = reinterpret_cast<T*>( buffer->_data ));
|
||||
}
|
||||
|
||||
// Change number of data bytes allocated to the buffer
|
||||
// Returns a new data pointer that may be different from the old data pointer
|
||||
static CAT_INLINE u8 *Resize(void *vdata, u32 data_bytes)
|
||||
{
|
||||
u8 *data = reinterpret_cast<u8*>( vdata );
|
||||
const u32 OVERHEAD_BYTES = (u32)(offsetof(AsyncBuffer, _tag));
|
||||
|
||||
if (!data) return Acquire(data_bytes);
|
||||
|
||||
AsyncBuffer *buffer = reinterpret_cast<AsyncBuffer*>( data - OVERHEAD_BYTES );
|
||||
|
||||
buffer = reinterpret_cast<AsyncBuffer*>(
|
||||
RegionAllocator::ii->Resize(buffer,
|
||||
OVERHEAD_BYTES + buffer->_tag_bytes + data_bytes) );
|
||||
|
||||
if (!buffer) return 0;
|
||||
|
||||
buffer->_data_bytes = data_bytes;
|
||||
|
||||
return (buffer->_data = buffer->_tag + buffer->_tag_bytes);
|
||||
}
|
||||
|
||||
public:
|
||||
// Wrap external data pointer
|
||||
static CAT_INLINE AsyncBuffer *Wrap(void *vdata, u32 data_bytes, u32 tag_bytes = 0)
|
||||
{
|
||||
u8 *data = reinterpret_cast<u8*>( vdata );
|
||||
const u32 OVERHEAD_BYTES = (u32)(offsetof(AsyncBuffer, _tag));
|
||||
|
||||
AsyncBuffer *buffer = reinterpret_cast<AsyncBuffer*>(
|
||||
RegionAllocator::ii->Acquire(OVERHEAD_BYTES + tag_bytes) );
|
||||
|
||||
if (!buffer) return 0;
|
||||
|
||||
buffer->_data_bytes = data_bytes;
|
||||
buffer->_tag_bytes = tag_bytes;
|
||||
buffer->_data = data;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public:
|
||||
// Only works on Acquired() buffers, not Wrap()ed buffers
|
||||
static CAT_INLINE AsyncBuffer *Promote(void *vdata)
|
||||
{
|
||||
u8 *data = reinterpret_cast<u8*>( vdata );
|
||||
const u32 OVERHEAD_BYTES = (u32)(offsetof(AsyncBuffer, _tag));
|
||||
|
||||
if (!data) return 0;
|
||||
return reinterpret_cast<AsyncBuffer*>( data - OVERHEAD_BYTES );
|
||||
}
|
||||
|
||||
public:
|
||||
// Release memory
|
||||
CAT_INLINE void Release()
|
||||
{
|
||||
RegionAllocator::ii->Release(this);
|
||||
}
|
||||
static CAT_INLINE void Release(AsyncBuffer *buffer)
|
||||
{
|
||||
RegionAllocator::ii->Release(buffer);
|
||||
}
|
||||
static CAT_INLINE void Release(void *vdata)
|
||||
{
|
||||
u8 *data = reinterpret_cast<u8*>( vdata );
|
||||
const u32 OVERHEAD_BYTES = (u32)(offsetof(AsyncBuffer, _tag));
|
||||
|
||||
if (!data) return;
|
||||
|
||||
AsyncBuffer *buffer = reinterpret_cast<AsyncBuffer*>( data - OVERHEAD_BYTES );
|
||||
|
||||
RegionAllocator::ii->Release(buffer);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // namespace cat
|
||||
|
||||
#endif // CAT_ASYNC_BUFFER_HPP
|
||||
56
DependentExtensions/cat/io/Base64.hpp
Normal file
56
DependentExtensions/cat/io/Base64.hpp
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
* Neither the name of LibCat nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef CAT_BASE64_HPP
|
||||
#define CAT_BASE64_HPP
|
||||
|
||||
#include <cat/Platform.hpp>
|
||||
#include <ostream>
|
||||
|
||||
namespace cat {
|
||||
|
||||
|
||||
// Write buffer to output stream in Base64 encoding
|
||||
int GetBase64LengthFromBinaryLength(int bytes);
|
||||
|
||||
// Returns number of bytes written, or 0 for error
|
||||
int WriteBase64(const void *buffer, int bytes, char *encoded_buffer, int encoded_bytes);
|
||||
int WriteBase64Str(const void *buffer, int bytes, char *encoded_buffer, int encoded_bytes); // This version writes a null-terminator
|
||||
int WriteBase64(const void *buffer, int bytes, std::ostream &output);
|
||||
|
||||
// Read buffer in Base64 encoding
|
||||
int GetBinaryLengthFromBase64Length(const char *encoded_buffer, int bytes);
|
||||
|
||||
// Returns number of bytes read, or 0 for error
|
||||
int ReadBase64(const char *encoded_buffer, int encoded_bytes, void *decoded_buffer, int decoded_bytes);
|
||||
int ReadBase64(const char *encoded_buffer, int encoded_bytes, std::ostream &output);
|
||||
|
||||
|
||||
} // namespace cat
|
||||
|
||||
#endif // CAT_BASE64_HPP
|
||||
194
DependentExtensions/cat/io/Logging.hpp
Normal file
194
DependentExtensions/cat/io/Logging.hpp
Normal file
@ -0,0 +1,194 @@
|
||||
/*
|
||||
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
* Neither the name of LibCat nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef CAT_LOGGING_HPP
|
||||
#define CAT_LOGGING_HPP
|
||||
|
||||
#include <cat/threads/RegionAllocator.hpp>
|
||||
|
||||
#if defined(CAT_OS_WINDOWS)
|
||||
#include <cat/port/WindowsInclude.hpp>
|
||||
#endif
|
||||
|
||||
namespace cat {
|
||||
|
||||
|
||||
//// Enumerations
|
||||
|
||||
enum EventSeverity
|
||||
{
|
||||
LVL_INANE,
|
||||
LVL_INFO,
|
||||
LVL_WARN,
|
||||
LVL_OOPS,
|
||||
LVL_FATAL,
|
||||
|
||||
LVL_SILENT, // invalid for an actual event's level, valid value for a threshold
|
||||
};
|
||||
|
||||
|
||||
//// Utility
|
||||
|
||||
region_string HexDumpString(const void *vdata, u32 bytes);
|
||||
|
||||
// Write to console (and debug log in windows) then trigger a breakpoint and exit
|
||||
void FatalStop(const char *message);
|
||||
|
||||
void DefaultLogCallback(EventSeverity severity, const char *source, region_ostringstream &msg);
|
||||
|
||||
|
||||
//// Logging
|
||||
|
||||
typedef void (*LogCallback)(EventSeverity severity, const char *source, region_ostringstream &msg);
|
||||
|
||||
class Logging : public Singleton<Logging>
|
||||
{
|
||||
CAT_SINGLETON(Logging);
|
||||
|
||||
LogCallback _callback;
|
||||
|
||||
friend class Recorder;
|
||||
void LogEvent(Recorder *recorder);
|
||||
|
||||
public:
|
||||
int _log_threshold;
|
||||
|
||||
public:
|
||||
void Initialize(EventSeverity min_severity = LVL_INANE);
|
||||
CAT_INLINE void SetThreshold(EventSeverity min_severity) { _log_threshold = min_severity; }
|
||||
void ReadSettings();
|
||||
void Shutdown();
|
||||
|
||||
protected:
|
||||
bool _service;
|
||||
#if defined(CAT_OS_WINDOWS)
|
||||
HANDLE _event_source;
|
||||
#endif
|
||||
|
||||
public:
|
||||
CAT_INLINE bool IsService() { return _service; }
|
||||
void EnableServiceMode(const char *service_name);
|
||||
void WriteServiceLog(EventSeverity severity, const char *line);
|
||||
|
||||
public:
|
||||
// Not thread-safe
|
||||
CAT_INLINE void SetLogCallback(LogCallback cb) { _callback = cb; }
|
||||
};
|
||||
|
||||
|
||||
//// Recorder
|
||||
|
||||
class Recorder
|
||||
{
|
||||
friend class Logging;
|
||||
EventSeverity _severity;
|
||||
const char *_subsystem;
|
||||
region_ostringstream _msg;
|
||||
|
||||
public:
|
||||
Recorder(const char *subsystem, EventSeverity severity);
|
||||
~Recorder();
|
||||
|
||||
public:
|
||||
template<class T> inline Recorder &operator<<(const T &t)
|
||||
{
|
||||
_msg << t;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
// Because there is an IF statement in the macro, you cannot use the
|
||||
// braceless if-else construction:
|
||||
// if (XYZ) WARN("SS") << "ERROR!"; else INFO("SS") << "OK!"; <-- bad
|
||||
// Instead use:
|
||||
// if (XYZ) { WARN("SS") << "ERROR!"; } else INFO("SS") << "OK!"; <-- good
|
||||
#define RECORD(subsystem, severity) \
|
||||
if (severity >= Logging::ii->_log_threshold) Recorder(subsystem, severity)
|
||||
|
||||
#define INANE(subsystem) RECORD(subsystem, LVL_INANE)
|
||||
#define INFO(subsystem) RECORD(subsystem, LVL_INFO)
|
||||
#define WARN(subsystem) RECORD(subsystem, LVL_WARN)
|
||||
#define OOPS(subsystem) RECORD(subsystem, LVL_OOPS)
|
||||
#define FATAL(subsystem) RECORD(subsystem, LVL_FATAL)
|
||||
|
||||
|
||||
//// Enforcer
|
||||
|
||||
class Enforcer
|
||||
{
|
||||
protected:
|
||||
std::ostringstream oss;
|
||||
|
||||
public:
|
||||
Enforcer(const char *locus);
|
||||
~Enforcer();
|
||||
|
||||
public:
|
||||
template<class T> inline Enforcer &operator<<(const T &t)
|
||||
{
|
||||
oss << t;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#define USE_ENFORCE_EXPRESSION_STRING
|
||||
#define USE_ENFORCE_FILE_LINE_STRING
|
||||
|
||||
|
||||
#if defined(USE_ENFORCE_EXPRESSION_STRING)
|
||||
# define ENFORCE_EXPRESSION_STRING(exp) "Failed assertion (" #exp ")"
|
||||
#else
|
||||
# define ENFORCE_EXPRESSION_STRING(exp) "Failed assertion"
|
||||
#endif
|
||||
|
||||
#if defined(USE_ENFORCE_FILE_LINE_STRING)
|
||||
# define ENFORCE_FILE_LINE_STRING " at " __FILE__ ":" CAT_STRINGIZE(__LINE__)
|
||||
#else
|
||||
# define ENFORCE_FILE_LINE_STRING ""
|
||||
#endif
|
||||
|
||||
// Because there is an IF statement in the macro, you cannot use the
|
||||
// braceless if-else construction:
|
||||
// if (XYZ) ENFORCE(A == B) << "ERROR"; else INFO("SS") << "OK"; <-- bad!
|
||||
// Instead use:
|
||||
// if (XYZ) { ENFORCE(A == B) << "ERROR"; } else INFO("SS") << "OK"; <-- good!
|
||||
|
||||
#define ENFORCE(exp) if ( (exp) == 0 ) Enforcer(ENFORCE_EXPRESSION_STRING(exp) ENFORCE_FILE_LINE_STRING "\n")
|
||||
#define EXCEPTION() Enforcer("Exception" ENFORCE_FILE_LINE_STRING "\n")
|
||||
|
||||
#if defined(CAT_DEBUG)
|
||||
# define DEBUG_ENFORCE(exp) ENFORCE(exp)
|
||||
#else
|
||||
# define DEBUG_ENFORCE(exp) if (0) ENFORCE(exp) /* hopefully will be optimized out of existence */
|
||||
#endif
|
||||
|
||||
|
||||
} // namespace cat
|
||||
|
||||
#endif // CAT_LOGGING_HPP
|
||||
75
DependentExtensions/cat/io/MMapFile.hpp
Normal file
75
DependentExtensions/cat/io/MMapFile.hpp
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
* Neither the name of LibCat nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef CAT_MMAPFILE_HPP
|
||||
#define CAT_MMAPFILE_HPP
|
||||
|
||||
#include <cat/Platform.hpp>
|
||||
|
||||
#if defined(CAT_OS_WINDOWS)
|
||||
# include <cat/port/WindowsInclude.hpp>
|
||||
#endif
|
||||
|
||||
namespace cat {
|
||||
|
||||
|
||||
class MMapFile
|
||||
{
|
||||
char *data;
|
||||
u32 len;
|
||||
s32 offset;
|
||||
|
||||
#if defined(CAT_OS_LINUX)
|
||||
int fd;
|
||||
#elif defined(CAT_OS_WINDOWS)
|
||||
HANDLE hFile, hMapping;
|
||||
#endif
|
||||
|
||||
public:
|
||||
MMapFile(const char *path);
|
||||
~MMapFile();
|
||||
|
||||
inline bool good() { return data != 0; }
|
||||
inline bool inside() { return offset >= 0 && offset < (s32)len; }
|
||||
|
||||
inline u32 size() { return len; }
|
||||
|
||||
inline void seek(s32 poffset) { offset = poffset; }
|
||||
inline bool underrun(s32 requested) { return (u32)(offset + requested) > len; }
|
||||
inline char *look() { return data + offset; }
|
||||
inline char *look(s32 offset) { return data + offset; }
|
||||
inline char *read(s32 requested) { offset += requested; return data + (offset - requested); }
|
||||
inline void skip(s32 requested) { offset += requested; }
|
||||
inline u32 remaining() { return len - offset; }
|
||||
inline u32 getOffset() { return offset; }
|
||||
};
|
||||
|
||||
|
||||
} // namespace cat
|
||||
|
||||
#endif // CAT_MMAPFILE_HPP
|
||||
107
DependentExtensions/cat/io/PackageManager.hpp
Normal file
107
DependentExtensions/cat/io/PackageManager.hpp
Normal file
@ -0,0 +1,107 @@
|
||||
/*
|
||||
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
* Neither the name of LibCat nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef CAT_PACKAGE_MANAGER_HPP
|
||||
#define CAT_PACKAGE_MANAGER_HPP
|
||||
|
||||
#include <cat/Platform.hpp>
|
||||
#include <cat/port/FastDelegate.h>
|
||||
|
||||
namespace cat {
|
||||
|
||||
|
||||
// Package resource identifier macro
|
||||
#define CAT_UNCAGE(packagePath, offset, size) offset, size
|
||||
|
||||
/*
|
||||
All file resources are packed into one large file and each is
|
||||
assigned a unique identifying number, starting from 0.
|
||||
|
||||
The client source code is preprocessed by a tool that replaces the
|
||||
arguments of the CAT_UNPACK() macro with the correct ID number based
|
||||
on the string given as the first argument.
|
||||
|
||||
CAT_UNPACK("world1/lightmap3.png")
|
||||
-> CAT_UNPACK("world1/lightmap3.png", 15241, 256, 0xdecryptionkey)
|
||||
|
||||
At runtime the client application will not be aware of the string
|
||||
name of a resource in the package, only where to go to get it.
|
||||
|
||||
Resources that are used together during tuning will have identifiers
|
||||
that are close together so that disk seek time is minimized.
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
Kennel files are simple concatenations of all of the game resources.
|
||||
|
||||
The goal is to reduce access time to data by cutting the operating
|
||||
system's file-system out completely. Furthermore, data that are
|
||||
accessed together are stored together on disk and in the same order
|
||||
that they are accessed.
|
||||
|
||||
Textures are compressed with modified JPEG-LS, providing the fastest
|
||||
possible access time.
|
||||
Sounds compression hasn't been investigated yet.
|
||||
|
||||
Each resource (sound/texture) is obfuscated with a 64-bit key, making
|
||||
it necessary to reverse-engineer the game client to decode in-game
|
||||
resources outside of the game.
|
||||
|
||||
The KennelFile object implements optimized algorithms for performing
|
||||
in-place modification to a large datafile (>4 GB).
|
||||
*/
|
||||
|
||||
class KennelPatchFile : AsyncFile
|
||||
{
|
||||
public:
|
||||
KennelPatchFile();
|
||||
~KennelPatchFile();
|
||||
|
||||
public:
|
||||
void Insert
|
||||
};
|
||||
|
||||
|
||||
// Kennel Patch Callback (param=bool: true on successful patch, false on error)
|
||||
typedef fastdelegate::FastDelegate1<bool, void> KennelPatchCallback;
|
||||
|
||||
class KennelFile : AsyncFile
|
||||
{
|
||||
public:
|
||||
KennelFile();
|
||||
virtual ~KennelFile();
|
||||
|
||||
public:
|
||||
bool Move(u64 dest, u32 region_size, u64 src, KennelPatchCallback OnComplete);
|
||||
};
|
||||
|
||||
|
||||
} // namespace cat
|
||||
|
||||
#endif // CAT_PACKAGE_MANAGER_HPP
|
||||
117
DependentExtensions/cat/io/Settings.hpp
Normal file
117
DependentExtensions/cat/io/Settings.hpp
Normal file
@ -0,0 +1,117 @@
|
||||
/*
|
||||
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
* Neither the name of LibCat nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef CAT_SETTINGS_HPP
|
||||
#define CAT_SETTINGS_HPP
|
||||
|
||||
#include <cat/Singleton.hpp>
|
||||
#include <cat/threads/Mutex.hpp>
|
||||
#include <fstream>
|
||||
|
||||
namespace cat {
|
||||
|
||||
|
||||
enum SettingsValueFlags
|
||||
{
|
||||
CAT_SETTINGS_FILLED = 1, // s[] array has been set
|
||||
CAT_SETTINGS_INT = 2, // value has been promoted to int 'i'
|
||||
};
|
||||
|
||||
struct SettingsValue
|
||||
{
|
||||
u8 flags; // sum of SettingsValueFlags
|
||||
char s[256]; // always nul-terminated
|
||||
int i;
|
||||
};
|
||||
|
||||
|
||||
class SettingsKey
|
||||
{
|
||||
public:
|
||||
SettingsKey(SettingsKey *lnode, SettingsKey *gnode, const char *name);
|
||||
~SettingsKey();
|
||||
|
||||
public:
|
||||
SettingsKey *lnode, *gnode;
|
||||
|
||||
char name[64]; // not necessarily nul-terminated
|
||||
|
||||
SettingsValue value;
|
||||
|
||||
public:
|
||||
void write(std::ofstream &file);
|
||||
};
|
||||
|
||||
|
||||
// User settings manager
|
||||
class Settings : public Singleton<Settings>
|
||||
{
|
||||
CAT_SINGLETON(Settings);
|
||||
|
||||
Mutex _lock;
|
||||
|
||||
protected:
|
||||
static const u32 KEY_HASH_SALT = 0xbaddecaf;
|
||||
static const int SETTINGS_HASH_BINS = 256;
|
||||
|
||||
SettingsKey *hbtrees[SETTINGS_HASH_BINS]; // hash table of binary trees
|
||||
|
||||
bool readSettings; // Flag set when settings have been read from disk
|
||||
bool modified; // Flag set when settings have been modified since last write
|
||||
|
||||
std::string _settings_file;
|
||||
|
||||
protected:
|
||||
SettingsKey *addKey(const char *name);
|
||||
SettingsKey *getKey(const char *name);
|
||||
|
||||
SettingsKey *initInt(const char *name, int n, bool overwrite);
|
||||
SettingsKey *initStr(const char *name, const char *value, bool overwrite);
|
||||
|
||||
void clear();
|
||||
|
||||
public:
|
||||
void readSettingsFromFile(const char *file_path = "settings.txt", const char *override_file = "override.txt");
|
||||
void readSettingsFromBuffer(const char *data, int len);
|
||||
void write();
|
||||
|
||||
public:
|
||||
int getInt(const char *name);
|
||||
const char *getStr(const char *name);
|
||||
|
||||
int getInt(const char *name, int init);
|
||||
const char *getStr(const char *name, const char *init);
|
||||
|
||||
void setInt(const char *name, int n);
|
||||
void setStr(const char *name, const char *value);
|
||||
};
|
||||
|
||||
|
||||
} // namespace cat
|
||||
|
||||
#endif // CAT_SETTINGS_HPP
|
||||
82
DependentExtensions/cat/io/ThreadPoolFiles.hpp
Normal file
82
DependentExtensions/cat/io/ThreadPoolFiles.hpp
Normal file
@ -0,0 +1,82 @@
|
||||
/*
|
||||
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
* Neither the name of LibCat nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef CAT_THREAD_POOL_FILES_HPP
|
||||
#define CAT_THREAD_POOL_FILES_HPP
|
||||
|
||||
#include <cat/threads/ThreadPool.hpp>
|
||||
#include <cat/port/FastDelegate.h>
|
||||
|
||||
#if defined(CAT_OS_WINDOWS)
|
||||
# include <cat/port/WindowsInclude.hpp>
|
||||
#endif
|
||||
|
||||
namespace cat {
|
||||
|
||||
|
||||
enum AsyncFileModes
|
||||
{
|
||||
ASYNCFILE_READ = 1,
|
||||
ASYNCFILE_WRITE = 2,
|
||||
ASYNCFILE_RANDOM = 4
|
||||
};
|
||||
|
||||
class AsyncFile : public ThreadRefObject
|
||||
{
|
||||
HANDLE _file;
|
||||
|
||||
protected:
|
||||
char _file_path[MAX_PATH+1];
|
||||
|
||||
public:
|
||||
AsyncFile(int priorityLevel);
|
||||
virtual ~AsyncFile();
|
||||
|
||||
public:
|
||||
bool Valid();
|
||||
const char *GetFilePath();
|
||||
|
||||
public:
|
||||
/*
|
||||
In read mode, Open() will fail if the file does not exist.
|
||||
In write mode, Open() will create the file if it does not exist.
|
||||
*/
|
||||
bool Open(const char *file_path, u32 async_file_modes);
|
||||
void Close();
|
||||
|
||||
bool SetSize(u64 bytes);
|
||||
u64 GetSize();
|
||||
|
||||
bool Read(AsyncBuffer *buffer, u64 offset, const AsyncCallback &callback);
|
||||
bool Write(AsyncBuffer *buffer, u64 offset, const AsyncCallback &callback = 0);
|
||||
};
|
||||
|
||||
|
||||
} // namespace cat
|
||||
|
||||
#endif // CAT_THREAD_POOL_FILES_HPP
|
||||
Reference in New Issue
Block a user