Index: ps/trunk/source/lib/res/h_mgr.cpp =================================================================== --- ps/trunk/source/lib/res/h_mgr.cpp (revision 26368) +++ ps/trunk/source/lib/res/h_mgr.cpp (nonexistent) @@ -1,789 +0,0 @@ -/* Copyright (C) 2019 Wildfire Games. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -/* - * handle manager for resources. - */ - -#include "precompiled.h" -#include "h_mgr.h" - -#include - -#include // CHAR_BIT -#include -#include -#include // std::bad_alloc - -#include "lib/fnv_hash.h" -#include "lib/allocators/overrun_protector.h" -#include "lib/allocators/pool.h" -#include "lib/module_init.h" - -#include - -namespace ERR { -static const Status H_IDX_INVALID = -120000; // totally invalid -static const Status H_IDX_UNUSED = -120001; // beyond current cap -static const Status H_TAG_MISMATCH = -120003; -static const Status H_TYPE_MISMATCH = -120004; -static const Status H_ALREADY_FREED = -120005; -} -static const StatusDefinition hStatusDefinitions[] = { - { ERR::H_IDX_INVALID, L"Handle index completely out of bounds" }, - { ERR::H_IDX_UNUSED, L"Handle index exceeds high-water mark" }, - { ERR::H_TAG_MISMATCH, L"Handle tag mismatch (stale reference?)" }, - { ERR::H_TYPE_MISMATCH, L"Handle type mismatch" }, - { ERR::H_ALREADY_FREED, L"Handle already freed" } -}; -STATUS_ADD_DEFINITIONS(hStatusDefinitions); - - - -// rationale -// -// why fixed size control blocks, instead of just allocating dynamically? -// it is expected that resources be created and freed often. this way is -// much nicer to the memory manager. defining control blocks larger than -// the allotted space is caught by h_alloc (made possible by the vtbl builder -// storing control block size). it is also efficient to have all CBs in an -// more or less contiguous array (see below). -// -// why a manager, instead of a simple pool allocator? -// we need a central list of resources for freeing at exit, checking if a -// resource has already been loaded (for caching), and when reloading. -// may as well keep them in an array, rather than add a list and index. - - - -// -// handle -// - -// 0 = invalid handle value -// < 0 is an error code (we assume < 0 <==> MSB is set - -// true for 1s and 2s complement and sign-magnitude systems) - -// fields: -// (shift value = # bits between LSB and field LSB. -// may be larger than the field type - only shift Handle vars!) - -// - index (0-based) of control block in our array. -// (field width determines maximum currently open handles) -#define IDX_BITS 16 -static const u64 IDX_MASK = (1l << IDX_BITS) - 1; - -// - tag (1-based) ensures the handle references a certain resource instance. -// (field width determines maximum unambiguous resource allocs) -using Tag = i64; -#define TAG_BITS 48 - -// make sure both fields fit within a Handle variable -cassert(IDX_BITS + TAG_BITS <= sizeof(Handle)*CHAR_BIT); - - -// return the handle's index field (always non-negative). -// no error checking! -static inline size_t h_idx(const Handle h) -{ - return (size_t)(h & IDX_MASK) - 1; -} - -// build a handle from index and tag. -// can't fail. -static inline Handle handle(size_t idx, u64 tag) -{ - const size_t idxPlusOne = idx+1; - ENSURE(idxPlusOne <= IDX_MASK); - ENSURE((tag & IDX_MASK) == 0); - Handle h = tag | idxPlusOne; - ENSURE(h > 0); - return h; -} - - -// -// internal per-resource-instance data -// - -// chosen so that all current resource structs are covered. -static const size_t HDATA_USER_SIZE = 104; - - -struct HDATA -{ - // we only need the tag, because it is trivial to compute - // &HDATA from idx and vice versa. storing the entire handle - // avoids needing to extract the tag field. - Handle h; // NB: will be overwritten by pool_free - - uintptr_t key; - - intptr_t refs; - - // smaller bit fields combined into 1 - // .. if set, do not actually release the resource (i.e. call dtor) - // when the handle is h_free-d, regardless of the refcount. - // set by h_alloc; reset on exit and by housekeeping. - u32 keep_open : 1; - // .. HACK: prevent adding to h_find lookup index if flags & RES_UNIQUE - // (because those handles might have several instances open, - // which the index can't currently handle) - u32 unique : 1; - u32 disallow_reload : 1; - - H_Type type; - - // for statistics - size_t num_derefs; - - // storing PIVFS here is not a good idea since this often isn't - // `freed' due to caching (and there is no dtor), so - // the VFS reference count would never reach zero. - VfsPath pathname; - - u8 user[HDATA_USER_SIZE]; -}; - - -// max data array entries. compared to last_in_use => signed. -static const ssize_t hdata_cap = (1ul << IDX_BITS)/4; - -// pool of fixed-size elements allows O(1) alloc and free; -// there is a simple mapping between HDATA address and index. -static Pool hpool; - - -// error checking strategy: -// all handles passed in go through h_data(Handle, Type) - - -// get a (possibly new) array entry. -// -// fails if idx is out of bounds. -static Status h_data_from_idx(ssize_t idx, HDATA*& hd) -{ - // don't check if idx is beyond the current high-water mark, because - // we might be allocating a new entry. subsequent tag checks protect - // against using unallocated entries. - if(size_t(idx) >= size_t(hdata_cap)) // also detects negative idx - WARN_RETURN(ERR::H_IDX_INVALID); - - hd = (HDATA*)(hpool.da.base + idx*hpool.el_size); - hd->num_derefs++; - return INFO::OK; -} - -static ssize_t h_idx_from_data(HDATA* hd) -{ - if(!pool_contains(&hpool, hd)) - WARN_RETURN(ERR::INVALID_POINTER); - return (uintptr_t(hd) - uintptr_t(hpool.da.base))/hpool.el_size; -} - - -// get HDATA for the given handle. -// only uses (and checks) the index field. -// used by h_force_close (which must work regardless of tag). -static inline Status h_data_no_tag(const Handle h, HDATA*& hd) -{ - ssize_t idx = (ssize_t)h_idx(h); - RETURN_STATUS_IF_ERR(h_data_from_idx(idx, hd)); - // need to verify it's in range - h_data_from_idx can only verify that - // it's < maximum allowable index. - if(uintptr_t(hd) > uintptr_t(hpool.da.base)+hpool.da.pos) - WARN_RETURN(ERR::H_IDX_UNUSED); - return INFO::OK; -} - - -static bool ignoreDoubleFree = false; - -// get HDATA for the given handle. -// also verifies the tag field. -// used by functions callable for any handle type, e.g. h_filename. -static inline Status h_data_tag(Handle h, HDATA*& hd) -{ - RETURN_STATUS_IF_ERR(h_data_no_tag(h, hd)); - - if(hd->key == 0) // HDATA was wiped out and hd->h overwritten by pool_free - { - if(ignoreDoubleFree) - return ERR::H_ALREADY_FREED; // NOWARN (see ignoreDoubleFree) - else - WARN_RETURN(ERR::H_ALREADY_FREED); - } - - if(h != hd->h) - WARN_RETURN(ERR::H_TAG_MISMATCH); - - return INFO::OK; -} - - -// get HDATA for the given handle. -// also verifies the type. -// used by most functions accessing handle data. -static Status h_data_tag_type(const Handle h, const H_Type type, HDATA*& hd) -{ - RETURN_STATUS_IF_ERR(h_data_tag(h, hd)); - - // h_alloc makes sure type isn't 0, so no need to check that here. - if(hd->type != type) - { - debug_printf("h_mgr: expected type %s, got %s\n", utf8_from_wstring(hd->type->name).c_str(), utf8_from_wstring(type->name).c_str()); - WARN_RETURN(ERR::H_TYPE_MISMATCH); - } - - return INFO::OK; -} - - -//----------------------------------------------------------------------------- -// lookup data structure -//----------------------------------------------------------------------------- - -// speed up h_find (called every h_alloc) -// multimap, because we want to add handles of differing type but same key -// (e.g. a VFile and Tex object for the same underlying filename hash key) -// -// store index because it's smaller and Handle can easily be reconstructed -// -// -// note: there may be several RES_UNIQUE handles of the same type and key -// (e.g. sound files - several instances of a sound definition file). -// that wasn't foreseen here, so we'll just refrain from adding to the index. -// that means they won't be found via h_find - no biggie. - -using Key2Idx = std::unordered_multimap; -using It = Key2Idx::iterator; -static OverrunProtector key2idx_wrapper; - -enum KeyRemoveFlag { KEY_NOREMOVE, KEY_REMOVE }; - -static Handle key_find(uintptr_t key, H_Type type, KeyRemoveFlag remove_option = KEY_NOREMOVE) -{ - Key2Idx* key2idx = key2idx_wrapper.get(); - if(!key2idx) - WARN_RETURN(ERR::NO_MEM); - - // initial return value: "not found at all, or it's of the - // wrong type". the latter happens when called by h_alloc to - // check if e.g. a Tex object already exists; at that time, - // only the corresponding VFile exists. - Handle ret = -1; - - std::pair range = key2idx->equal_range(key); - for(It it = range.first; it != range.second; ++it) - { - ssize_t idx = it->second; - HDATA* hd; - if(h_data_from_idx(idx, hd) != INFO::OK) - continue; - if(hd->type != type || hd->key != key) - continue; - - // found a match - if(remove_option == KEY_REMOVE) - key2idx->erase(it); - ret = hd->h; - break; - } - - key2idx_wrapper.lock(); - return ret; -} - - -static void key_add(uintptr_t key, Handle h) -{ - Key2Idx* key2idx = key2idx_wrapper.get(); - if(!key2idx) - return; - - const ssize_t idx = h_idx(h); - // note: MSDN documentation of stdext::hash_multimap is incorrect; - // there is no overload of insert() that returns pair. - (void)key2idx->insert(std::make_pair(key, idx)); - - key2idx_wrapper.lock(); -} - - -static void key_remove(uintptr_t key, H_Type type) -{ - Handle ret = key_find(key, type, KEY_REMOVE); - ENSURE(ret > 0); -} - - -//---------------------------------------------------------------------------- -// h_alloc -//---------------------------------------------------------------------------- - -static void warn_if_invalid(HDATA* hd) -{ -#ifndef NDEBUG - H_VTbl* vtbl = hd->type; - - // validate HDATA - // currently nothing to do; is checked by h_alloc and - // the others have no invariants we could check. - - // have the resource validate its user_data - Status err = vtbl->validate(hd->user); - ENSURE(err == INFO::OK); - - // make sure empty space in control block isn't touched - // .. but only if we're not storing a filename there - const u8* start = hd->user + vtbl->user_size; - const u8* end = hd->user + HDATA_USER_SIZE; - for(const u8* p = start; p < end; p++) - ENSURE(*p == 0); // else: handle user data was overrun! -#else - UNUSED2(hd); -#endif -} - - -static Status type_validate(H_Type type) -{ - if(!type) - WARN_RETURN(ERR::INVALID_PARAM); - if(type->user_size > HDATA_USER_SIZE) - WARN_RETURN(ERR::LIMIT); - if(type->name == 0) - WARN_RETURN(ERR::INVALID_PARAM); - - return INFO::OK; -} - - -static Tag gen_tag() -{ - static Tag tag; - tag += (1ull << IDX_BITS); - // it's not easy to detect overflow, because compilers - // are allowed to assume it'll never happen. however, - // pow(2, 64-IDX_BITS) is "enough" anyway. - return tag; -} - - -static Handle reuse_existing_handle(uintptr_t key, H_Type type, size_t flags) -{ - if(flags & RES_NO_CACHE) - return 0; - - // object of specified key and type doesn't exist yet - Handle h = h_find(type, key); - if(h <= 0) - return 0; - - HDATA* hd; - RETURN_STATUS_IF_ERR(h_data_tag_type(h, type, hd)); // h_find means this won't fail - - hd->refs += 1; - - // we are reactivating a closed but cached handle. - // need to generate a new tag so that copies of the - // previous handle can no longer access the resource. - // (we don't need to reset the tag in h_free, because - // use before this fails due to refs > 0 check in h_user_data). - if(hd->refs == 1) - { - const Tag tag = gen_tag(); - h = handle(h_idx(h), tag); // can't fail - hd->h = h; - } - - return h; -} - - -static Status call_init_and_reload(Handle h, H_Type type, HDATA* hd, const PIVFS& vfs, const VfsPath& pathname, va_list* init_args) -{ - Status err = INFO::OK; - H_VTbl* vtbl = type; // exact same thing but for clarity - - // init - if(vtbl->init) - vtbl->init(hd->user, *init_args); - - // reload - if(vtbl->reload) - { - // catch exception to simplify reload funcs - let them use new() - try - { - err = vtbl->reload(hd->user, vfs, pathname, h); - if(err == INFO::OK) - warn_if_invalid(hd); - } - catch(std::bad_alloc&) - { - err = ERR::NO_MEM; - } - } - - return err; -} - - -static Handle alloc_new_handle(H_Type type, const PIVFS& vfs, const VfsPath& pathname, uintptr_t key, size_t flags, va_list* init_args) -{ - HDATA* hd = (HDATA*)pool_alloc(&hpool, 0); - if(!hd) - WARN_RETURN(ERR::NO_MEM); - new(&hd->pathname) VfsPath; - - ssize_t idx = h_idx_from_data(hd); - RETURN_STATUS_IF_ERR(idx); - - // (don't want to do this before the add-reference exit, - // so as not to waste tags for often allocated handles.) - const Tag tag = gen_tag(); - Handle h = handle(idx, tag); // can't fail. - - hd->h = h; - hd->key = key; - hd->type = type; - hd->refs = 1; - if(!(flags & RES_NO_CACHE)) - hd->keep_open = 1; - if(flags & RES_DISALLOW_RELOAD) - hd->disallow_reload = 1; - hd->unique = (flags & RES_UNIQUE) != 0; - hd->pathname = pathname; - - if(key && !hd->unique) - key_add(key, h); - - Status err = call_init_and_reload(h, type, hd, vfs, pathname, init_args); - if(err < 0) - goto fail; - - return h; - -fail: - // reload failed; free the handle - hd->keep_open = 0; // disallow caching (since contents are invalid) - (void)h_free(h, type); // (h_free already does WARN_IF_ERR) - - // note: since some uses will always fail (e.g. loading sounds if - // g_Quickstart), do not complain here. - return (Handle)err; -} - - -static std::recursive_mutex h_mutex; - -// any further params are passed to type's init routine -Handle h_alloc(H_Type type, const PIVFS& vfs, const VfsPath& pathname, size_t flags, ...) -{ - std::lock_guard lock(h_mutex); - - RETURN_STATUS_IF_ERR(type_validate(type)); - - const uintptr_t key = fnv_hash(pathname.string().c_str(), pathname.string().length()*sizeof(pathname.string()[0])); - - // see if we can reuse an existing handle - Handle h = reuse_existing_handle(key, type, flags); - RETURN_STATUS_IF_ERR(h); - // .. successfully reused the handle; refcount increased - if(h > 0) - return h; - // .. need to allocate a new one: - va_list args; - va_start(args, flags); - h = alloc_new_handle(type, vfs, pathname, key, flags, &args); - va_end(args); - return h; // alloc_new_handle already does WARN_RETURN_STATUS_IF_ERR -} - - -//----------------------------------------------------------------------------- - -static void h_free_hd(HDATA* hd) -{ - if(hd->refs > 0) - hd->refs--; - - // still references open or caching requests it stays - do not release. - if(hd->refs > 0 || hd->keep_open) - return; - - // actually release the resource (call dtor, free control block). - - // h_alloc makes sure type != 0; if we get here, it still is - H_VTbl* vtbl = hd->type; - - // call its destructor - // note: H_TYPE_DEFINE currently always defines a dtor, but play it safe - if(vtbl->dtor) - vtbl->dtor(hd->user); - - if(hd->key && !hd->unique) - key_remove(hd->key, hd->type); - -#ifndef NDEBUG - // to_string is slow for some handles, so avoid calling it if unnecessary - if(debug_filter_allows("H_MGR|")) - { - wchar_t buf[H_STRING_LEN]; - if(vtbl->to_string(hd->user, buf) < 0) - wcscpy_s(buf, ARRAY_SIZE(buf), L"(error)"); - debug_printf("H_MGR| free %s %s accesses=%lu %s\n", utf8_from_wstring(hd->type->name).c_str(), hd->pathname.string8().c_str(), (unsigned long)hd->num_derefs, utf8_from_wstring(buf).c_str()); - } -#endif - - hd->pathname.~VfsPath(); // FIXME: ugly hack, but necessary to reclaim memory - memset(hd, 0, sizeof(*hd)); - pool_free(&hpool, hd); -} - - -Status h_free(Handle& h, H_Type type) -{ - std::lock_guard lock(h_mutex); - - // 0-initialized or an error code; don't complain because this - // happens often and is harmless. - if(h <= 0) - return INFO::OK; - - // wipe out the handle to prevent reuse but keep a copy for below. - const Handle h_copy = h; - h = 0; - - HDATA* hd; - RETURN_STATUS_IF_ERR(h_data_tag_type(h_copy, type, hd)); - - h_free_hd(hd); - return INFO::OK; -} - - -//---------------------------------------------------------------------------- -// remaining API - -void* h_user_data(const Handle h, const H_Type type) -{ - HDATA* hd; - if(h_data_tag_type(h, type, hd) != INFO::OK) - return 0; - - if(!hd->refs) - { - // note: resetting the tag is not enough (user might pass in its value) - DEBUG_WARN_ERR(ERR::LOGIC); // no references to resource (it's cached, but someone is accessing it directly) - return 0; - } - - warn_if_invalid(hd); - return hd->user; -} - - -VfsPath h_filename(const Handle h) -{ - // don't require type check: should be usable for any handle, - // even if the caller doesn't know its type. - HDATA* hd; - if(h_data_tag(h, hd) != INFO::OK) - return VfsPath(); - return hd->pathname; -} - - -// TODO: what if iterating through all handles is too slow? -Status h_reload(const PIVFS& vfs, const VfsPath& pathname) -{ - std::lock_guard lock(h_mutex); - - const u32 key = fnv_hash(pathname.string().c_str(), pathname.string().length()*sizeof(pathname.string()[0])); - - // destroy (note: not free!) all handles backed by this file. - // do this before reloading any of them, because we don't specify reload - // order (the parent resource may be reloaded first, and load the child, - // whose original data would leak). - for(HDATA* hd = (HDATA*)hpool.da.base; hd < (HDATA*)(hpool.da.base + hpool.da.pos); hd = (HDATA*)(uintptr_t(hd)+hpool.el_size)) - { - if(hd->key == 0 || hd->key != key || hd->disallow_reload) - continue; - hd->type->dtor(hd->user); - } - - Status ret = INFO::OK; - - // now reload all affected handles - size_t i = 0; - for(HDATA* hd = (HDATA*)hpool.da.base; hd < (HDATA*)(hpool.da.base + hpool.da.pos); hd = (HDATA*)(uintptr_t(hd)+hpool.el_size), i++) - { - if(hd->key == 0 || hd->key != key || hd->disallow_reload) - continue; - - Status err = hd->type->reload(hd->user, vfs, hd->pathname, hd->h); - // don't stop if an error is encountered - try to reload them all. - if(err < 0) - { - h_free(hd->h, hd->type); - if(ret == 0) // don't overwrite first error - ret = err; - } - else - warn_if_invalid(hd); - } - - return ret; -} - - -Handle h_find(H_Type type, uintptr_t key) -{ - std::lock_guard lock(h_mutex); - return key_find(key, type); -} - - - -// force the resource to be freed immediately, even if cached. -// tag is not checked - this allows the first Handle returned -// (whose tag will change after being 'freed', but remaining in memory) -// to later close the object. -// this is used when reinitializing the sound engine - -// at that point, all (cached) OpenAL resources must be freed. -Status h_force_free(Handle h, H_Type type) -{ - std::lock_guard lock(h_mutex); - - // require valid index; ignore tag; type checked below. - HDATA* hd; - RETURN_STATUS_IF_ERR(h_data_no_tag(h, hd)); - if(hd->type != type) - WARN_RETURN(ERR::H_TYPE_MISMATCH); - hd->keep_open = 0; - hd->refs = 0; - h_free_hd(hd); - return INFO::OK; -} - - -// increment Handle 's reference count. -// only meant to be used for objects that free a Handle in their dtor, -// so that they are copy-equivalent and can be stored in a STL container. -// do not use this to implement refcounting on top of the Handle scheme, -// e.g. loading a Handle once and then passing it around. instead, have each -// user load the resource; refcounting is done under the hood. -void h_add_ref(Handle h) -{ - HDATA* hd; - if(h_data_tag(h, hd) != INFO::OK) - return; - - ENSURE(hd->refs); // if there are no refs, how did the caller manage to keep a Handle?! - hd->refs++; -} - - -// retrieve the internal reference count or a negative error code. -// background: since h_alloc has no way of indicating whether it -// allocated a new handle or reused an existing one, counting references -// within resource control blocks is impossible. since that is sometimes -// necessary (always wrapping objects in Handles is excessive), we -// provide access to the internal reference count. -intptr_t h_get_refcnt(Handle h) -{ - HDATA* hd; - RETURN_STATUS_IF_ERR(h_data_tag(h, hd)); - - ENSURE(hd->refs); // if there are no refs, how did the caller manage to keep a Handle?! - return hd->refs; -} - - -static ModuleInitState initState; - -static Status Init() -{ - RETURN_STATUS_IF_ERR(pool_create(&hpool, hdata_cap*sizeof(HDATA), sizeof(HDATA))); - return INFO::OK; -} - -static void Shutdown() -{ - debug_printf("H_MGR| shutdown. any handle frees after this are leaks!\n"); - // objects that store handles to other objects are destroyed before their - // children, so the subsequent forced destruction of the child here will - // raise a double-free warning unless we ignore it. (#860, #915, #920) - ignoreDoubleFree = true; - - std::lock_guard lock(h_mutex); - - // forcibly close all open handles - for(HDATA* hd = (HDATA*)hpool.da.base; hd < (HDATA*)(hpool.da.base + hpool.da.pos); hd = (HDATA*)(uintptr_t(hd)+hpool.el_size)) - { - // it's already been freed; don't free again so that this - // doesn't look like an error. - if(hd->key == 0) - continue; - - // disable caching; we need to release the resource now. - hd->keep_open = 0; - hd->refs = 0; - - h_free_hd(hd); - } - - pool_destroy(&hpool); -} - -void h_mgr_free_type(const H_Type type) -{ - ignoreDoubleFree = true; - - std::lock_guard lock(h_mutex); - - // forcibly close all open handles of the specified type - for(HDATA* hd = (HDATA*)hpool.da.base; hd < (HDATA*)(hpool.da.base + hpool.da.pos); hd = (HDATA*)(uintptr_t(hd)+hpool.el_size)) - { - // free if not previously freed and only free the proper type - if (hd->key == 0 || hd->type != type) - continue; - - // disable caching; we need to release the resource now. - hd->keep_open = 0; - hd->refs = 0; - - h_free_hd(hd); - } -} - -void h_mgr_init() -{ - ModuleInit(&initState, Init); -} - -void h_mgr_shutdown() -{ - ModuleShutdown(&initState, Shutdown); -} Property changes on: ps/trunk/source/lib/res/h_mgr.cpp ___________________________________________________________________ Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Index: ps/trunk/source/lib/res/handle.h =================================================================== --- ps/trunk/source/lib/res/handle.h (revision 26368) +++ ps/trunk/source/lib/res/handle.h (nonexistent) @@ -1,43 +0,0 @@ -/* Copyright (C) 2010 Wildfire Games. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -/* - * forward declaration of Handle (reduces dependencies) - */ - -#ifndef INCLUDED_HANDLE -#define INCLUDED_HANDLE - -/** - * `handle' representing a reference to a resource (sound, texture, etc.) - * - * 0 is the (silently ignored) invalid handle value; < 0 is an error code. - * - * this is 64 bits because we want tags to remain unique. (tags are a - * counter that disambiguate several subsequent uses of the same - * resource array slot). 32-bit handles aren't enough because the index - * field requires at least 12 bits, thus leaving only about 512K possible - * tag values. - **/ -typedef i64 Handle; - -#endif // #ifndef INCLUDED_HANDLE Property changes on: ps/trunk/source/lib/res/handle.h ___________________________________________________________________ Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Index: ps/trunk/source/lib/res/h_mgr.h =================================================================== --- ps/trunk/source/lib/res/h_mgr.h (revision 26368) +++ ps/trunk/source/lib/res/h_mgr.h (nonexistent) @@ -1,432 +0,0 @@ -/* Copyright (C) 2010 Wildfire Games. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -/* - * handle manager for resources. - */ - -/* - -[KEEP IN SYNC WITH WIKI] - -introduction ------------- - -a resource is an instance of a specific type of game data (e.g. texture), -described by a control block (example fields: format, pointer to tex data). - -this module allocates storage for the control blocks, which are accessed -via handle. it also provides support for transparently reloading resources -from disk (allows in-game editing of data), and caches resource data. -finally, it frees all resources at exit, preventing leaks. - - -handles -------- - -handles are an indirection layer between client code and resources -(represented by their control blocks, which contains/points to its data). -they allow an important check not possible with a direct pointer: -guaranteeing the handle references a given resource /instance/. - -problem: code C1 allocates a resource, and receives a pointer p to its -control block. C1 passes p on to C2, and later frees it. -now other code allocates a resource, and happens to reuse the free slot -pointed to by p (also possible if simply allocating from the heap). -when C2 accesses p, the pointer is valid, but we cannot tell that -it is referring to a resource that had already been freed. big trouble. - -solution: each allocation receives a unique tag (a global counter that -is large enough to never overflow). Handles include this tag, as well -as a reference (array index) to the control block, which isn't directly -accessible. when dereferencing the handle, we check if the handle's tag -matches the copy stored in the control block. this protects against stale -handle reuse, double-free, and accidentally referencing other resources. - -type: each handle has an associated type. these must be checked to prevent -using textures as sounds, for example. with the manual vtbl scheme, -this type is actually a pointer to the resource object's vtbl, and is -set up via H_TYPE_DEFINE. this means that types are private to the module -that declared the handle; knowledge of the type ensures the caller -actually declared, and owns the resource. - - -guide to defining and using resources -------------------------------------- - -1) choose a name for the resource, used to represent all resources -of this type. we will call ours "Res1"; all below occurrences of this -must be replaced with the actual name (exact spelling). -why? the vtbl builder defines its functions as e.g. Res1_reload; -your actual definition must match. - -2) declare its control block: -struct Res1 -{ - void* data; // data loaded from file - size_t flags; // set when resource is created -}; - -Note that all control blocks are stored in fixed-size slots -(HDATA_USER_SIZE bytes), so squeezing the size of your data doesn't -help unless yours is the largest. - -3) build its vtbl: -H_TYPE_DEFINE(Res1); - -this defines the symbol H_Res1, which is used whenever the handle -manager needs its type. it is only accessible to this module -(file scope). note that it is actually a pointer to the vtbl. -this must come before uses of H_Res1, and after the CB definition; -there are no restrictions WRT functions, because the macro -forward-declares what it needs. - -4) implement all 'virtual' functions from the resource interface. -note that inheritance isn't really possible with this approach - -all functions must be defined, even if not needed. - --- - -init: -one-time init of the control block. called from h_alloc. -precondition: control block is initialized to 0. - -static void Type_init(Res1* r, va_list args) -{ - r->flags = va_arg(args, int); -} - -if the caller of h_alloc passed additional args, they are available -in args. if init references more args than were passed, big trouble. -however, this is a bug in your code, and cannot be triggered -maliciously. only your code knows the resource type, and it is the -only call site of h_alloc. -there is no provision for indicating failure. if one-time init fails -(rare, but one example might be failure to allocate memory that is -for the lifetime of the resource, instead of in reload), it will -have to set the control block state such that reload will fail. - --- - -reload: -does all initialization of the resource that requires its source file. -called after init; also after dtor every time the file is reloaded. - -static Status Type_reload(Res1* r, const VfsPath& pathname, Handle); -{ - // already loaded; done - if(r->data) - return 0; - - r->data = malloc(100); - if(!r->data) - WARN_RETURN(ERR::NO_MEM); - // (read contents of into r->data) - return 0; -} - -reload must abort if the control block data indicates the resource -has already been loaded! example: if texture's reload is called first, -it loads itself from file (triggering file.reload); afterwards, -file.reload will be called again. we can't avoid this, because the -handle manager doesn't know anything about dependencies -(here, texture -> file). -return value: 0 if successful (includes 'already loaded'), -negative error code otherwise. if this fails, the resource is freed -(=> dtor is called!). - -note that any subsequent changes to the resource state must be -stored in the control block and 'replayed' when reloading. -example: when uploading a texture, store the upload parameters -(filter, internal format); when reloading, upload again accordingly. - --- - -dtor: -frees all data allocated by init and reload. called after reload has -indicated failure, before reloading a resource, after h_free, -or at exit (if the resource is still extant). -except when reloading, the control block will be zeroed afterwards. - -static void Type_dtor(Res1* r); -{ - free(r->data); -} - -again no provision for reporting errors - there's no one to act on it -if called at exit. you can ENSURE or log the error, though. - -be careful to correctly handle the different cases in which this routine -can be called! some flags should persist across reloads (e.g. choices made -during resource init time that must remain valid), while everything else -*should be zeroed manually* (to behave correctly when reloading). -be advised that this interface may change; a "prepare for reload" method -or "compact/free extraneous resources" may be added. - --- - -validate: -makes sure the resource control block is in a valid state. returns 0 if -all is well, or a negative error code. -called automatically when the Handle is dereferenced or freed. - -static Status Type_validate(const Res1* r); -{ - const int permissible_flags = 0x01; - if(debug_IsPointerBogus(r->data)) - WARN_RETURN(ERR::_1); - if(r->flags & ~permissible_flags) - WARN_RETURN(ERR::_2); - return 0; -} - - -5) provide your layer on top of the handle manager: -Handle res1_load(const VfsPath& pathname, int my_flags) -{ - // passes my_flags to init - return h_alloc(H_Res1, pathname, 0, my_flags); -} - -Status res1_free(Handle& h) -{ - // control block is automatically zeroed after this. - return h_free(h, H_Res1); -} - -(this layer allows a res_load interface on top of all the loaders, -and is necessary because your module is the only one that knows H_Res1). - -6) done. the resource will be freed at exit (if not done already). - -here's how to access the control block, given a : -a) - H_DEREF(h, Res1, r); - -creates a variable r of type Res1*, which points to the control block -of the resource referenced by h. returns "invalid handle" -(a negative error code) on failure. -b) - Res1* r = h_user_data(h, H_Res1); - if(!r) - ; // bail - -useful if H_DEREF's error return (of type signed integer) isn't -acceptable. otherwise, prefer a) - this is pretty clunky, and -we could switch H_DEREF to throwing an exception on error. - -*/ - -#ifndef INCLUDED_H_MGR -#define INCLUDED_H_MGR - -// do not include from public header files! -// handle.h declares type Handle, and avoids making -// everything dependent on this (rather often updated) header. - - -#include // type init routines get va_list of args - -#ifndef INCLUDED_HANDLE -#include "handle.h" -#endif - -#include "lib/file/vfs/vfs.h" - -extern void h_mgr_init(); -extern void h_mgr_shutdown(); - - -// handle type (for 'type safety' - can't use a texture handle as a sound) - -// registering extension for each module is bad - some may use many -// (e.g. texture - many formats). -// handle manager shouldn't know about handle types - - -/* -///xxx advantage of manual vtbl: -no boilerplate init, h_alloc calls ctor directly, make sure it fits in the memory slot -vtbl contains sizeof resource data, and name! -but- has to handle variable params, a bit ugly -*/ - -// 'manual vtbl' type id -// handles have a type, to prevent using e.g. texture handles as a sound. -// -// alternatives: -// - enum of all handle types (smaller, have to pass all methods to h_alloc) -// - class (difficult to compare type, handle manager needs to know of all users) -// -// checked in h_alloc: -// - user_size must fit in what the handle manager provides -// - name must not be 0 -// -// init: user data is initially zeroed -// dtor: user data is zeroed afterwards -// reload: if this resource type is opened by another resource's reload, -// our reload routine MUST check if already opened! This is relevant when -// a file is reloaded: if e.g. a sound object opens a file, the handle -// manager calls the reload routines for the 2 handles in unspecified order. -// ensuring the order would require a tag field that can't overflow - -// not really guaranteed with 32-bit handles. it'd also be more work -// to sort the handles by creation time, or account for several layers of -// dependencies. -struct H_VTbl -{ - void (*init)(void* user, va_list); - Status (*reload)(void* user, const PIVFS& vfs, const VfsPath& pathname, Handle); - void (*dtor)(void* user); - Status (*validate)(const void* user); - Status (*to_string)(const void* user, wchar_t* buf); - size_t user_size; - const wchar_t* name; -}; - -typedef H_VTbl* H_Type; - -#define H_TYPE_DEFINE(type)\ - /* forward decls */\ - static void type##_init(type*, va_list);\ - static Status type##_reload(type*, const PIVFS&, const VfsPath&, Handle);\ - static void type##_dtor(type*);\ - static Status type##_validate(const type*);\ - static Status type##_to_string(const type*, wchar_t* buf);\ - static H_VTbl V_##type =\ - {\ - (void (*)(void*, va_list))type##_init,\ - (Status (*)(void*, const PIVFS&, const VfsPath&, Handle))type##_reload,\ - (void (*)(void*))type##_dtor,\ - (Status (*)(const void*))type##_validate,\ - (Status (*)(const void*, wchar_t*))type##_to_string,\ - sizeof(type), /* control block size */\ - WIDEN(#type) /* name */\ - };\ - static H_Type H_##type = &V_##type - - // note: we cast to void* pointers so the functions can be declared to - // take the control block pointers, instead of requiring a cast in each. - // the forward decls ensure the function signatures are correct. - - -// convenience macro for h_user_data: -// casts its return value to the control block type. -// use if H_DEREF's returning a negative error code isn't acceptable. -#define H_USER_DATA(h, type) (type*)h_user_data(h, H_##type) - -// even more convenient wrapper for h_user_data: -// declares a pointer (), assigns it H_USER_DATA, and has -// the user's function return a negative error code on failure. -// -// note: don't use STMT - var decl must be visible to "caller" -#define H_DEREF(h, type, var)\ - /* h already indicates an error - return immediately to pass back*/\ - /* that specific error, rather than only ERR::INVALID_HANDLE*/\ - if(h < 0)\ - WARN_RETURN((Status)h);\ - type* const var = H_USER_DATA(h, type);\ - if(!var)\ - WARN_RETURN(ERR::INVALID_HANDLE); - - -// all functions check the passed tag (part of the handle) and type against -// the internal values. if they differ, an error is returned. - - - - -// h_alloc flags -enum -{ - // alias for RES_TEMP scope. the handle will not be kept open. - RES_NO_CACHE = 0x01, - - // not cached, and will never reuse a previous instance - RES_UNIQUE = RES_NO_CACHE|0x10, - - // object is requesting it never be reloaded (e.g. because it's not - // backed by a file) - RES_DISALLOW_RELOAD = 0x20 -}; - -const size_t H_STRING_LEN = 256; - - - -// allocate a new handle. -// if key is 0, or a (key, type) handle doesn't exist, -// some free entry is used. -// otherwise, a handle to the existing object is returned, -// and HDATA.size != 0. -//// user_size is checked to make sure the user data fits in the handle data space. -// dtor is associated with type and called when the object is freed. -// handle data is initialized to 0; optionally, a pointer to it is returned. -extern Handle h_alloc(H_Type type, const PIVFS& vfs, const VfsPath& pathname, size_t flags = 0, ...); -extern Status h_free(Handle& h, H_Type type); - - -// Forcibly frees all handles of a specified type. -void h_mgr_free_type(const H_Type type); - - -// find and return a handle by key (typically filename hash) -// currently O(log n). -// -// HACK: currently can't find RES_UNIQUE handles, because there -// may be multiple instances of them, breaking the lookup data structure. -extern Handle h_find(H_Type type, uintptr_t key); - -// returns a void* pointer to the control block of the resource , -// or 0 on error (i.e. h is invalid or of the wrong type). -// prefer using H_DEREF or H_USER_DATA. -extern void* h_user_data(Handle h, H_Type type); - -extern VfsPath h_filename(Handle h); - - -extern Status h_reload(const PIVFS& vfs, const VfsPath& pathname); - -// force the resource to be freed immediately, even if cached. -// tag is not checked - this allows the first Handle returned -// (whose tag will change after being 'freed', but remaining in memory) -// to later close the object. -// this is used when reinitializing the sound engine - -// at that point, all (cached) OpenAL resources must be freed. -extern Status h_force_free(Handle h, H_Type type); - -// increment Handle 's reference count. -// only meant to be used for objects that free a Handle in their dtor, -// so that they are copy-equivalent and can be stored in a STL container. -// do not use this to implement refcounting on top of the Handle scheme, -// e.g. loading a Handle once and then passing it around. instead, have each -// user load the resource; refcounting is done under the hood. -extern void h_add_ref(Handle h); - -// retrieve the internal reference count or a negative error code. -// background: since h_alloc has no way of indicating whether it -// allocated a new handle or reused an existing one, counting references -// within resource control blocks is impossible. since that is sometimes -// necessary (always wrapping objects in Handles is excessive), we -// provide access to the internal reference count. -extern intptr_t h_get_refcnt(Handle h); - -#endif // #ifndef INCLUDED_H_MGR Property changes on: ps/trunk/source/lib/res/h_mgr.h ___________________________________________________________________ Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Index: ps/trunk/source/graphics/MapReader.h =================================================================== --- ps/trunk/source/graphics/MapReader.h (revision 26368) +++ ps/trunk/source/graphics/MapReader.h (revision 26369) @@ -1,182 +1,181 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #ifndef INCLUDED_MAPREADER #define INCLUDED_MAPREADER #include "MapIO.h" #include "graphics/LightEnv.h" -#include "lib/res/handle.h" #include "ps/CStr.h" #include "ps/FileIo.h" #include "scriptinterface/ScriptTypes.h" #include "simulation2/system/Entity.h" class CTerrain; class WaterManager; class SkyManager; class CLightEnv; class CCinemaManager; class CPostprocManager; class CTriggerManager; class CSimulation2; class CSimContext; class CTerrainTextureEntry; class CGameView; class CXMLReader; class CMapGenerator; class ScriptContext; class ScriptInterface; class CMapReader : public CMapIO { friend class CXMLReader; public: // constructor CMapReader(); ~CMapReader(); // LoadMap: try to load the map from given file; reinitialise the scene to new data if successful void LoadMap(const VfsPath& pathname, const ScriptContext& cx, JS::HandleValue settings, CTerrain*, WaterManager*, SkyManager*, CLightEnv*, CGameView*, CCinemaManager*, CTriggerManager*, CPostprocManager* pPostproc, CSimulation2*, const CSimContext*, int playerID, bool skipEntities); void LoadRandomMap(const CStrW& scriptFile, const ScriptContext& cx, JS::HandleValue settings, CTerrain*, WaterManager*, SkyManager*, CLightEnv*, CGameView*, CCinemaManager*, CTriggerManager*, CPostprocManager* pPostproc_, CSimulation2*, int playerID); private: // Load script settings for use by scripts int LoadScriptSettings(); // load player settings only int LoadPlayerSettings(); // load map settings only int LoadMapSettings(); // UnpackTerrain: unpack the terrain from the input stream int UnpackTerrain(); // UnpackCinema: unpack the cinematic tracks from the input stream int UnpackCinema(); // UnpackMap: unpack the given data from the raw data stream into local variables int UnpackMap(); // ApplyData: take all the input data, and rebuild the scene from it int ApplyData(); int ApplyTerrainData(); // read some misc data from the XML file int ReadXML(); // read entity data from the XML file int ReadXMLEntities(); // Copy random map settings over to sim int LoadRMSettings(); // Generate random map int GenerateMap(); // Parse script data into terrain int ParseTerrain(); // Parse script data into entities int ParseEntities(); // Parse script data into environment int ParseEnvironment(); // Parse script data into camera int ParseCamera(); // size of map ssize_t m_PatchesPerSide; // heightmap for map std::vector m_Heightmap; // list of terrain textures used by map std::vector m_TerrainTextures; // tile descriptions for each tile std::vector m_Tiles; // lightenv stored in file CLightEnv m_LightEnv; // startup script CStrW m_Script; // random map data CStrW m_ScriptFile; JS::PersistentRootedValue m_ScriptSettings; JS::PersistentRootedValue m_MapData; CMapGenerator* m_MapGen; CFileUnpacker unpacker; CTerrain* pTerrain; WaterManager* pWaterMan; SkyManager* pSkyMan; CPostprocManager* pPostproc; CLightEnv* pLightEnv; CGameView* pGameView; CCinemaManager* pCinema; CTriggerManager* pTrigMan; CSimulation2* pSimulation2; const CSimContext* pSimContext; int m_PlayerID; bool m_SkipEntities; VfsPath filename_xml; bool only_xml; u32 file_format_version; entity_id_t m_StartingCameraTarget; CVector3D m_StartingCamera; // UnpackTerrain generator state size_t cur_terrain_tex; size_t num_terrain_tex; CXMLReader* xml_reader; }; /** * A restricted map reader that returns various summary information * for use by scripts (particularly the GUI). */ class CMapSummaryReader { public: /** * Try to load a map file. * @param pathname Path to .pmp or .xml file */ PSRETURN LoadMap(const VfsPath& pathname); /** * Returns a value of the form: * @code * { * "settings": { ... contents of the map's ... } * } * @endcode */ void GetMapSettings(const ScriptInterface& scriptInterface, JS::MutableHandleValue); private: CStr m_ScriptSettings; }; #endif Index: ps/trunk/source/graphics/MiniPatch.h =================================================================== --- ps/trunk/source/graphics/MiniPatch.h (revision 26368) +++ ps/trunk/source/graphics/MiniPatch.h (revision 26369) @@ -1,47 +1,43 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ /* * Definition of a single terrain tile */ #ifndef INCLUDED_MINIPATCH #define INCLUDED_MINIPATCH -#include "lib/res/handle.h" - class CTerrainTextureEntry; -/////////////////////////////////////////////////////////////////////////////// // CMiniPatch: definition of a single terrain tile class CMiniPatch { public: // constructor CMiniPatch(); // texture applied to tile CTerrainTextureEntry* Tex; // 'priority' of the texture - determines drawing order of terrain textures int Priority; CTerrainTextureEntry* GetTextureEntry() { return Tex; } int GetPriority() { return Priority; } }; - -#endif +#endif // INCLUDED_MINIPATCH Index: ps/trunk/source/graphics/TerrainTextureManager.h =================================================================== --- ps/trunk/source/graphics/TerrainTextureManager.h (revision 26368) +++ ps/trunk/source/graphics/TerrainTextureManager.h (revision 26369) @@ -1,143 +1,142 @@ /* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #ifndef INCLUDED_TERRAINTEXTUREMANAGER #define INCLUDED_TERRAINTEXTUREMANAGER #include "lib/file/vfs/vfs_path.h" -#include "lib/res/handle.h" #include "ps/CStr.h" #include "ps/Singleton.h" #include "renderer/backend/gl/DeviceCommandContext.h" #include "renderer/backend/gl/Texture.h" #include #include #include // access to sole CTerrainTextureManager object #define g_TexMan CTerrainTextureManager::GetSingleton() #define NUM_ALPHA_MAPS 14 class CTerrainTextureEntry; class CTerrainProperties; typedef std::shared_ptr CTerrainPropertiesPtr; class CTerrainGroup { // name of this terrain group (as specified by the terrain XML) CStr m_Name; // "index".. basically a bogus integer that can be used by ScEd to set texture // priorities size_t m_Index; // list of textures of this type (found from the texture directory) std::vector m_Terrains; public: CTerrainGroup(CStr name, size_t index): m_Name(name), m_Index(index) {} // Add a texture entry to this terrain type void AddTerrain(CTerrainTextureEntry*); // Remove a texture entry void RemoveTerrain(CTerrainTextureEntry*); size_t GetIndex() const { return m_Index; } CStr GetName() const { return m_Name; } const std::vector &GetTerrains() const { return m_Terrains; } }; struct TerrainAlpha { // Composite alpha map (all the alpha maps packed into one texture). std::unique_ptr m_CompositeAlphaMap; // Data is used to separate file loading and uploading to GPU. std::shared_ptr m_CompositeDataToUpload; // Coordinates of each (untransformed) alpha map within the packed texture. struct { float u0, u1, v0, v1; } m_AlphaMapCoords[NUM_ALPHA_MAPS]; }; /////////////////////////////////////////////////////////////////////////////////////////// // CTerrainTextureManager : manager class for all terrain texture objects class CTerrainTextureManager : public Singleton { friend class CTerrainTextureEntry; public: using TerrainGroupMap = std::map; using TerrainAlphaMap = std::map; // constructor, destructor CTerrainTextureManager(); ~CTerrainTextureManager(); // Find all XML's in the directory (with subdirs) and try to load them as // terrain XML's int LoadTerrainTextures(); void UnloadTerrainTextures(); CTerrainTextureEntry* FindTexture(const CStr& tag) const; // Create a texture object for a new terrain texture at path, using the // property sheet props. CTerrainTextureEntry* AddTexture(const CTerrainPropertiesPtr& props, const VfsPath& path); // Remove the texture from all our maps and lists and delete it afterwards. void DeleteTexture(CTerrainTextureEntry* entry); // Find or create a new texture group. All terrain groups are owned by the // texturemanager (TerrainTypeManager) CTerrainGroup* FindGroup(const CStr& name); const TerrainGroupMap& GetGroups() const { return m_TerrainGroups; } CTerrainTextureManager::TerrainAlphaMap::iterator LoadAlphaMap(const VfsPath& alphaMapType); void UploadResourcesIfNeeded(Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext); private: // All texture entries created by this class, for easy freeing now that // textures may be in several STextureType's std::vector m_TextureEntries; TerrainGroupMap m_TerrainGroups; TerrainAlphaMap m_TerrainAlphas; size_t m_LastGroupIndex; // A way to separate file loading and uploading to GPU to not stall uploading. // Once we get a properly threaded loading we might optimize that. std::vector m_AlphaMapsToUpload; }; #endif // INCLUDED_TERRAINTEXTUREMANAGER Index: ps/trunk/source/graphics/tests/test_TextureManager.h =================================================================== --- ps/trunk/source/graphics/tests/test_TextureManager.h (revision 26368) +++ ps/trunk/source/graphics/tests/test_TextureManager.h (revision 26369) @@ -1,128 +1,123 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "lib/self_test.h" #include "graphics/TextureManager.h" #include "lib/external_libraries/libsdl.h" #include "lib/file/vfs/vfs.h" -#include "lib/res/h_mgr.h" #include "lib/tex/tex.h" #include "lib/ogl.h" #include "ps/XML/Xeromyces.h" class TestTextureManager : public CxxTest::TestSuite { PIVFS m_VFS; public: void setUp() { DeleteDirectory(DataDir()/"_testcache"); // clean up in case the last test run failed m_VFS = CreateVfs(); TS_ASSERT_OK(m_VFS->Mount(L"", DataDir() / "mods" / "_test.tex" / "", VFS_MOUNT_MUST_EXIST)); TS_ASSERT_OK(m_VFS->Mount(L"cache/", DataDir() / "_testcache" / "", 0, VFS_MAX_PRIORITY)); - h_mgr_init(); - CXeromyces::Startup(); } void tearDown() { CXeromyces::Terminate(); - h_mgr_shutdown(); - m_VFS.reset(); DeleteDirectory(DataDir()/"_testcache"); } void test_load_basic() { { CTextureManager texman(m_VFS, false, true); CTexturePtr t1 = texman.CreateTexture(CTextureProperties(L"art/textures/a/demo.png")); TS_ASSERT(!t1->IsLoaded()); TS_ASSERT(!t1->TryLoad()); TS_ASSERT(!t1->IsLoaded()); TS_ASSERT(texman.MakeProgress()); for (size_t i = 0; i < 100; ++i) { if (texman.MakeProgress()) break; SDL_Delay(10); } TS_ASSERT(t1->IsLoaded()); // We can't test sizes because we had to disable GL function calls // and therefore couldn't load the texture. Maybe we should try loading // the texture file directly, to make sure it's actually worked. // TS_ASSERT_EQUALS(t1->GetWidth(), (size_t)64); // TS_ASSERT_EQUALS(t1->GetHeight(), (size_t)64); // CreateTexture should return the same object CTexturePtr t2 = texman.CreateTexture(CTextureProperties(L"art/textures/a/demo.png")); TS_ASSERT(t1 == t2); } // New texture manager - should use the cached file { CTextureManager texman(m_VFS, false, true); CTexturePtr t1 = texman.CreateTexture(CTextureProperties(L"art/textures/a/demo.png")); TS_ASSERT(!t1->IsLoaded()); TS_ASSERT(t1->TryLoad()); TS_ASSERT(t1->IsLoaded()); } } void test_load_formats() { CTextureManager texman(m_VFS, false, true); CTexturePtr t1 = texman.CreateTexture(CTextureProperties(L"art/textures/a/demo.tga")); CTexturePtr t2 = texman.CreateTexture(CTextureProperties(L"art/textures/a/demo-abgr.dds")); CTexturePtr t3 = texman.CreateTexture(CTextureProperties(L"art/textures/a/demo-dxt1.dds")); CTexturePtr t4 = texman.CreateTexture(CTextureProperties(L"art/textures/a/demo-dxt5.dds")); t1->TryLoad(); t2->TryLoad(); t3->TryLoad(); t4->TryLoad(); size_t done = 0; for (size_t i = 0; i < 100; ++i) { if (texman.MakeProgress()) ++done; if (done == 8) // 4 loads, 4 conversions break; SDL_Delay(10); } TS_ASSERT(t1->IsLoaded()); TS_ASSERT(t2->IsLoaded()); TS_ASSERT(t3->IsLoaded()); TS_ASSERT(t4->IsLoaded()); } }; Index: ps/trunk/source/lib/tex/tex.h =================================================================== --- ps/trunk/source/lib/tex/tex.h (revision 26368) +++ ps/trunk/source/lib/tex/tex.h (revision 26369) @@ -1,451 +1,450 @@ /* Copyright (C) 2022 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* * read/write 2d texture files; allows conversion between pixel formats * and automatic orientation correction. */ /** Introduction ------------ This module allows reading/writing 2d images in various file formats and encapsulates them in Tex objects. It supports converting between pixel formats; this is to an extent done automatically when reading/writing. Provision is also made for flipping all images to a default orientation. Format Conversion ----------------- Image file formats have major differences in their native pixel format: some store in BGR order, or have rows arranged bottom-up. We must balance runtime cost/complexity and convenience for the application (not dumping the entire problem on its lap). That means rejecting really obscure formats (e.g. right-to-left pixels), but converting everything else to uncompressed RGB "plain" format except where noted in enum TexFlags (1). Note: conversion is implemented as a pipeline: e.g. "DDS decompress + vertical flip" would be done by decompressing to RGB (DDS codec) and then flipping (generic transform). This is in contrast to all<->all conversion paths: that would be much more complex, if more efficient. Since any kind of preprocessing at runtime is undesirable (the absolute priority is minimizing load time), prefer file formats that are close to the final pixel format. 1) one of the exceptions is S3TC compressed textures. glCompressedTexImage2D requires these be passed in their original format; decompressing would be counterproductive. In this and similar cases, TexFlags indicates such deviations from the plain format. Default Orientation ------------------- After loading, all images (except DDS, because its orientation is indeterminate) are automatically converted to the global row orientation: top-down or bottom-up, as specified by tex_set_global_orientation. If that isn't called, the default is top-down to match Photoshop's DDS output (since this is meant to be the no-preprocessing-required optimized format). Reasons to change it might be to speed up loading bottom-up BMP or TGA images, or to match OpenGL's convention for convenience; however, be aware of the abovementioned issues with DDS. Rationale: it is not expected that this will happen at the renderer layer (a 'flip all texcoords' flag is too much trouble), so the application would have to do the same anyway. By taking care of it here, we unburden the app and save time, since some codecs (e.g. PNG) can flip for free when loading. Codecs / IO Implementation -------------------------- To ease adding support for new formats, they are organized as codecs. The interface aims to minimize code duplication, so it's organized following the principle of "Template Method" - this module both calls into codecs, and provides helper functions that they use. IO is done via VFS, but the codecs are decoupled from this and work with memory buffers. Access to them is endian-safe. When "writing", the image is put into an expandable memory region. This supports external libraries like libpng that do not know the output size beforehand, but avoids the need for a buffer between library and IO layer. Read and write are zero-copy. **/ #ifndef INCLUDED_TEX #define INCLUDED_TEX -#include "lib/res/handle.h" #include "lib/os_path.h" #include "lib/file/vfs/vfs_path.h" #include "lib/allocators/dynarray.h" #include namespace ERR { const Status TEX_UNKNOWN_FORMAT = -120100; const Status TEX_INCOMPLETE_HEADER = -120101; const Status TEX_FMT_INVALID = -120102; const Status TEX_INVALID_COLOR_TYPE = -120103; const Status TEX_NOT_8BIT_PRECISION = -120104; const Status TEX_INVALID_LAYOUT = -120105; const Status TEX_COMPRESSED = -120106; const Status TEX_INVALID_SIZE = -120107; } namespace WARN { const Status TEX_INVALID_DATA = +120108; } namespace INFO { const Status TEX_CODEC_CANNOT_HANDLE = +120109; } /** * flags describing the pixel format. these are to be interpreted as * deviations from "plain" format, i.e. uncompressed RGB. **/ enum TexFlags { /** * flags & TEX_DXT is a field indicating compression. * if 0, the texture is uncompressed; * otherwise, it holds the S3TC type: 1,3,5 or DXT1A. * not converted by default - glCompressedTexImage2D receives * the compressed data. **/ TEX_DXT = 0x7, // mask /** * we need a special value for DXT1a to avoid having to consider * flags & TEX_ALPHA to determine S3TC type. * the value is arbitrary; do not rely on it! **/ DXT1A = 7, /** * indicates B and R pixel components are exchanged. depending on * flags & TEX_ALPHA or bpp, this means either BGR or BGRA. * not converted by default - it's an acceptable format for OpenGL. **/ TEX_BGR = 0x08, /** * indicates the image contains an alpha channel. this is set for * your convenience - there are many formats containing alpha and * divining this information from them is hard. * (conversion is not applicable here) **/ TEX_ALPHA = 0x10, /** * indicates the image is 8bpp greyscale. this is required to * differentiate between alpha-only and intensity formats. * not converted by default - it's an acceptable format for OpenGL. **/ TEX_GREY = 0x20, /** * flags & TEX_ORIENTATION is a field indicating orientation, * i.e. in what order the pixel rows are stored. * * tex_load always sets this to the global orientation * (and flips the image accordingly to match). * texture codecs may in intermediate steps during loading set this * to 0 if they don't know which way around they are (e.g. DDS), * or to whatever their file contains. **/ TEX_BOTTOM_UP = 0x40, TEX_TOP_DOWN = 0x80, TEX_ORIENTATION = TEX_BOTTOM_UP|TEX_TOP_DOWN, /// mask /** * indicates the image data includes mipmaps. they are stored from lowest * to highest (1x1), one after the other. * (conversion is not applicable here) **/ TEX_MIPMAPS = 0x100, TEX_UNDEFINED_FLAGS = ~0x1FF }; /** * Stores all data describing an image. * TODO: rename to TextureData. **/ class Tex { public: struct MIPLevel { // A pointer to the mip level image data (pixels). u8* data; u32 dataSize; u32 width; u32 height; }; /** * file buffer or image data. note: during the course of transforms * (which may occur when being loaded), this may be replaced with * a new buffer (e.g. if decompressing file contents). **/ std::shared_ptr m_Data; size_t m_DataSize; /** * offset to image data in file. this is required since * tex_get_data needs to return the pixels, but data * returns the actual file buffer. zero-copy load and * write-back to file is also made possible. **/ size_t m_Ofs; size_t m_Width; size_t m_Height; size_t m_Bpp; /// see TexFlags and "Format Conversion" in docs. size_t m_Flags; ~Tex() { free(); } /** * Is the texture object valid and self-consistent? * * @return Status **/ Status validate() const; /** * free all resources associated with the image and make further * use of it impossible. * * @return Status **/ void free(); /** * decode an in-memory texture file into texture object. * * FYI, currently BMP, TGA, JPG, JP2, PNG, DDS are supported - but don't * rely on this (not all codecs may be included). * * @param data Input data. * @param data_size Its size [bytes]. * @return Status. **/ Status decode(const std::shared_ptr& data, size_t data_size); /** * encode a texture into a memory buffer in the desired file format. * * @param extension (including '.'). * @param da Output memory array. Allocated here; caller must free it * when no longer needed. Invalid unless function succeeds. * @return Status **/ Status encode(const OsPath& extension, DynArray* da); /** * store the given image data into a Tex object; this will be as if * it had been loaded via tex_load. * * rationale: support for in-memory images is necessary for * emulation of glCompressedTexImage2D and useful overall. * however, we don't want to provide an alternate interface for each API; * these would have to be changed whenever fields are added to Tex. * instead, provide one entry point for specifying images. * note: since we do not know how \ was allocated, the caller must free * it themselves (after calling tex_free, which is required regardless of * alloc type). * * we need only add bookkeeping information and "wrap" it in * our Tex struct, hence the name. * * @param w,h Pixel dimensions. * @param bpp Bits per pixel. * @param flags TexFlags. * @param data Img texture data. note: size is calculated from other params. * @param ofs * @return Status **/ Status wrap(size_t w, size_t h, size_t bpp, size_t flags, const std::shared_ptr& data, size_t ofs); // // modify image // /** * Change the pixel format. * * @param transforms TexFlags that are to be flipped. * @return Status **/ Status transform(size_t transforms); /** * Change the pixel format (2nd version) * (note: this is equivalent to Tex::transform(t, t-\>flags^new_flags). * * @param new_flags desired new value of TexFlags. * @return Status **/ Status transform_to(size_t new_flags); // // return image information // /** * return a pointer to the image data (pixels), taking into account any * header(s) that may come before it. * * @return pointer to the data. **/ u8* get_data(); const std::vector& GetMIPLevels() const { return m_MIPLevels; } /** * return the ARGB value of the 1x1 mipmap level of the texture. * * @return ARGB value (or 0 if texture does not have mipmaps) **/ u32 get_average_color() const; /** * return total byte size of the image pixels. (including mipmaps!) * rationale: this is preferable to calculating manually because it's * less error-prone (e.g. confusing bits_per_pixel with bytes). * * @return size [bytes] **/ size_t img_size() const; private: void UpdateMIPLevels(); std::vector m_MIPLevels; }; /** * Set the orientation to which all loaded images will * automatically be converted (excepting file formats that don't specify * their orientation, i.e. DDS). See "Default Orientation" in docs. * @param orientation Either TEX_BOTTOM_UP or TEX_TOP_DOWN. **/ extern void tex_set_global_orientation(int orientation); /** * special value for levels_to_skip: the callback will only be called * for the base mipmap level (i.e. 100%) **/ const int TEX_BASE_LEVEL_ONLY = -1; /** * callback function for each mipmap level. * * @param level number; 0 for base level (i.e. 100%), or the first one * in case some were skipped. * @param level_w, level_h pixel dimensions (powers of 2, never 0) * @param level_data the level's texels * @param level_data_size [bytes] * @param cbData passed through from tex_util_foreach_mipmap. **/ typedef void (*MipmapCB)(size_t level, size_t level_w, size_t level_h, const u8* RESTRICT level_data, size_t level_data_size, void* RESTRICT cbData); /** * for a series of mipmaps stored from base to highest, call back for * each level. * * @param w,h Pixel dimensions. * @param bpp Bits per pixel. * @param data Series of mipmaps. * @param levels_to_skip Number of levels (counting from base) to skip, or * TEX_BASE_LEVEL_ONLY to only call back for the base image. * Rationale: this avoids needing to special case for images with or * without mipmaps. * @param data_padding Minimum pixel dimensions of mipmap levels. * This is used in S3TC images, where each level is actually stored in * 4x4 blocks. usually 1 to indicate levels are consecutive. * @param cb MipmapCB to call. * @param cbData Extra data to pass to cb. **/ extern void tex_util_foreach_mipmap(size_t w, size_t h, size_t bpp, const u8* data, int levels_to_skip, size_t data_padding, MipmapCB cb, void* RESTRICT cbData); // // image writing // /** * Is the file's extension that of a texture format supported by tex_load? * * Rationale: tex_load complains if the given file is of an * unsupported type. this API allows users to preempt that warning * (by checking the filename themselves), and also provides for e.g. * enumerating only images in a file picker. * an alternative might be a flag to suppress warning about invalid files, * but this is open to misuse. * * @param pathname Only the extension (starting with '.') is used. case-insensitive. * @return bool **/ extern bool tex_is_known_extension(const VfsPath& pathname); /** * return the minimum header size (i.e. offset to pixel data) of the * file format corresponding to the filename. * * rationale: this can be used to optimize calls to tex_write: when * allocating the buffer that will hold the image, allocate this much * extra and pass the pointer as base+hdr_size. this allows writing the * header directly into the output buffer and makes for zero-copy IO. * * @param filename Filename; only the extension (that after '.') is used. * case-insensitive. * @return size [bytes] or 0 on error (i.e. no codec found). **/ extern size_t tex_hdr_size(const VfsPath& filename); #endif // INCLUDED_TEX Index: ps/trunk/source/ps/Filesystem.cpp =================================================================== --- ps/trunk/source/ps/Filesystem.cpp (revision 26368) +++ ps/trunk/source/ps/Filesystem.cpp (revision 26369) @@ -1,172 +1,169 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" + #include "Filesystem.h" +#include "lib/sysdep/dir_watch.h" +#include "lib/utf8.h" #include "ps/CLogger.h" #include "ps/CStr.h" #include "ps/Profile.h" -#include "lib/res/h_mgr.h" // h_reload -#include "lib/sysdep/dir_watch.h" -#include "lib/utf8.h" - #include PIVFS g_VFS; static std::vector > g_ReloadFuncs; bool VfsFileExists(const VfsPath& pathname) { return g_VFS->GetFileInfo(pathname, 0) == INFO::OK; } bool VfsDirectoryExists(const VfsPath& pathname) { ENSURE(pathname.IsDirectory()); return g_VFS->GetDirectoryEntries(pathname, NULL, NULL) == INFO::OK; } void RegisterFileReloadFunc(FileReloadFunc func, void* obj) { g_ReloadFuncs.emplace_back(func, obj); } void UnregisterFileReloadFunc(FileReloadFunc func, void* obj) { g_ReloadFuncs.erase(std::remove(g_ReloadFuncs.begin(), g_ReloadFuncs.end(), std::make_pair(func, obj))); } // try to skip unnecessary work by ignoring uninteresting notifications. static bool CanIgnore(const DirWatchNotification& notification) { // ignore directories const OsPath& pathname = notification.Pathname(); if(pathname.IsDirectory()) return true; // ignore uninteresting file types (e.g. temp files, or the // hundreds of XMB files that are generated from XML) const OsPath extension = pathname.Extension(); const wchar_t* extensionsToIgnore[] = { L".xmb", L".tmp" }; for(size_t i = 0; i < ARRAY_SIZE(extensionsToIgnore); i++) { if(extension == extensionsToIgnore[i]) return true; } return false; } Status ReloadChangedFiles() { PROFILE3("hotload"); std::vector notifications; RETURN_STATUS_IF_ERR(dir_watch_Poll(notifications)); for(size_t i = 0; i < notifications.size(); i++) { if(!CanIgnore(notifications[i])) { VfsPath pathname; RETURN_STATUS_IF_ERR(g_VFS->GetVirtualPath(notifications[i].Pathname(), pathname)); RETURN_STATUS_IF_ERR(g_VFS->RemoveFile(pathname)); RETURN_STATUS_IF_ERR(g_VFS->RepopulateDirectory(pathname.Parent()/"")); // Tell each hotloadable system about this file change: for (size_t j = 0; j < g_ReloadFuncs.size(); ++j) g_ReloadFuncs[j].first(g_ReloadFuncs[j].second, pathname); - - RETURN_STATUS_IF_ERR(h_reload(g_VFS, pathname)); } } return INFO::OK; } std::wstring GetWstringFromWpath(const fs::wpath& path) { #if BOOST_FILESYSTEM_VERSION == 3 return path.wstring(); #else return path.string(); #endif } CVFSFile::CVFSFile() : m_BufferSize(0) { } CVFSFile::~CVFSFile() { } PSRETURN CVFSFile::Load(const PIVFS& vfs, const VfsPath& filename, bool log /* = true */) { // Load should never be called more than once, so complain if (m_Buffer) { DEBUG_WARN_ERR(ERR::LOGIC); return PSRETURN_CVFSFile_AlreadyLoaded; } Status ret = vfs->LoadFile(filename, m_Buffer, m_BufferSize); if (ret != INFO::OK) { if (log) LOGERROR("CVFSFile: file %s couldn't be opened (vfs_load: %lld)", filename.string8(), (long long)ret); m_Buffer.reset(); m_BufferSize = 0; return PSRETURN_CVFSFile_LoadFailed; } return PSRETURN_OK; } const u8* CVFSFile::GetBuffer() const { return m_Buffer.get(); } size_t CVFSFile::GetBufferSize() const { return m_BufferSize; } CStr CVFSFile::GetAsString() const { return std::string((char*)GetBuffer(), GetBufferSize()); } CStr CVFSFile::DecodeUTF8() const { const u8* buffer = GetBuffer(); // Detect if there's a UTF-8 BOM and strip it if (GetBufferSize() >= 3 && buffer[0] == 0xEF && buffer[1] == 0xBB && buffer[2] == 0xBF) { return std::string(&buffer[3], buffer + GetBufferSize()); } else { return std::string(buffer, buffer + GetBufferSize()); } } Index: ps/trunk/source/ps/GameSetup/GameSetup.cpp =================================================================== --- ps/trunk/source/ps/GameSetup/GameSetup.cpp (revision 26368) +++ ps/trunk/source/ps/GameSetup/GameSetup.cpp (revision 26369) @@ -1,1294 +1,1286 @@ /* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "ps/GameSetup/GameSetup.h" #include "graphics/GameView.h" #include "graphics/MapReader.h" #include "graphics/TerrainTextureManager.h" #include "gui/CGUI.h" #include "gui/GUIManager.h" #include "i18n/L10n.h" #include "lib/app_hooks.h" #include "lib/config2.h" #include "lib/external_libraries/libsdl.h" #include "lib/file/common/file_stats.h" #include "lib/input.h" -#include "lib/ogl.h" -#include "lib/res/h_mgr.h" #include "lib/timer.h" #include "lobby/IXmppClient.h" #include "network/NetServer.h" #include "network/NetClient.h" #include "network/NetMessage.h" #include "network/NetMessages.h" #include "ps/CConsole.h" #include "ps/CLogger.h" #include "ps/ConfigDB.h" #include "ps/Filesystem.h" #include "ps/Game.h" #include "ps/GameSetup/Atlas.h" #include "ps/GameSetup/Paths.h" #include "ps/GameSetup/Config.h" #include "ps/GameSetup/CmdLineArgs.h" #include "ps/GameSetup/HWDetect.h" #include "ps/Globals.h" #include "ps/GUID.h" #include "ps/Hotkey.h" #include "ps/Joystick.h" #include "ps/Loader.h" #include "ps/Mod.h" #include "ps/ModIo.h" #include "ps/Profile.h" #include "ps/ProfileViewer.h" #include "ps/Profiler2.h" #include "ps/Pyrogenesis.h" // psSetLogDir #include "ps/scripting/JSInterface_Console.h" #include "ps/TouchInput.h" #include "ps/UserReport.h" #include "ps/Util.h" #include "ps/VideoMode.h" #include "ps/VisualReplay.h" #include "ps/World.h" #include "renderer/Renderer.h" #include "renderer/SceneRenderer.h" #include "renderer/VertexBufferManager.h" #include "scriptinterface/FunctionWrapper.h" #include "scriptinterface/JSON.h" #include "scriptinterface/ScriptInterface.h" #include "scriptinterface/ScriptStats.h" #include "scriptinterface/ScriptContext.h" #include "scriptinterface/ScriptConversions.h" #include "simulation2/Simulation2.h" #include "soundmanager/scripting/JSInterface_Sound.h" #include "soundmanager/ISoundManager.h" #include "tools/atlas/GameInterface/GameLoop.h" #if !(OS_WIN || OS_MACOSX || OS_ANDROID) // assume all other platforms use X11 for wxWidgets #define MUST_INIT_X11 1 #include #else #define MUST_INIT_X11 0 #endif extern void RestartEngine(); #include #include #include #include #include ERROR_GROUP(System); ERROR_TYPE(System, SDLInitFailed); ERROR_TYPE(System, VmodeFailed); ERROR_TYPE(System, RequiredExtensionsMissing); thread_local std::shared_ptr g_ScriptContext; bool g_InDevelopmentCopy; bool g_CheckedIfInDevelopmentCopy = false; ErrorReactionInternal psDisplayError(const wchar_t* UNUSED(text), size_t UNUSED(flags)) { // If we're fullscreen, then sometimes (at least on some particular drivers on Linux) // displaying the error dialog hangs the desktop since the dialog box is behind the // fullscreen window. So we just force the game to windowed mode before displaying the dialog. // (But only if we're in the main thread, and not if we're being reentrant.) if (Threading::IsMainThread()) { static bool reentering = false; if (!reentering) { reentering = true; g_VideoMode.SetFullscreen(false); reentering = false; } } // We don't actually implement the error display here, so return appropriately return ERI_NOT_IMPLEMENTED; } void MountMods(const Paths& paths, const std::vector& mods) { OsPath modPath = paths.RData()/"mods"; OsPath modUserPath = paths.UserData()/"mods"; size_t userFlags = VFS_MOUNT_WATCH|VFS_MOUNT_ARCHIVABLE; size_t baseFlags = userFlags|VFS_MOUNT_MUST_EXIST; size_t priority = 0; for (size_t i = 0; i < mods.size(); ++i) { priority = i + 1; // Mods are higher priority than regular mountings, which default to priority 0 OsPath modName(mods[i]); // Only mount mods from the user path if they don't exist in the 'rdata' path. if (DirectoryExists(modPath / modName / "")) g_VFS->Mount(L"", modPath / modName / "", baseFlags, priority); else g_VFS->Mount(L"", modUserPath / modName / "", userFlags, priority); } // Mount the user mod last. In dev copy, mount it with a low priority. Otherwise, make it writable. g_VFS->Mount(L"", modUserPath / "user" / "", userFlags, InDevelopmentCopy() ? 0 : priority + 1); } static void InitVfs(const CmdLineArgs& args, int flags) { TIMER(L"InitVfs"); const bool setup_error = (flags & INIT_HAVE_DISPLAY_ERROR) == 0; const Paths paths(args); OsPath logs(paths.Logs()); CreateDirectories(logs, 0700); psSetLogDir(logs); // desired location for crashlog is now known. update AppHooks ASAP // (particularly before the following error-prone operations): AppHooks hooks = {0}; hooks.bundle_logs = psBundleLogs; hooks.get_log_dir = psLogDir; if (setup_error) hooks.display_error = psDisplayError; app_hooks_update(&hooks); g_VFS = CreateVfs(); const OsPath readonlyConfig = paths.RData()/"config"/""; // Mount these dirs with highest priority so that mods can't overwrite them. g_VFS->Mount(L"cache/", paths.Cache(), VFS_MOUNT_ARCHIVABLE, VFS_MAX_PRIORITY); // (adding XMBs to archive speeds up subsequent reads) if (readonlyConfig != paths.Config()) g_VFS->Mount(L"config/", readonlyConfig, 0, VFS_MAX_PRIORITY-1); g_VFS->Mount(L"config/", paths.Config(), 0, VFS_MAX_PRIORITY); g_VFS->Mount(L"screenshots/", paths.UserData()/"screenshots"/"", 0, VFS_MAX_PRIORITY); g_VFS->Mount(L"saves/", paths.UserData()/"saves"/"", VFS_MOUNT_WATCH, VFS_MAX_PRIORITY); // Engine localization files (regular priority, these can be overwritten). g_VFS->Mount(L"l10n/", paths.RData()/"l10n"/""); // Mods will be mounted later. // note: don't bother with g_VFS->TextRepresentation - directories // haven't yet been populated and are empty. } static void InitPs(bool setup_gui, const CStrW& gui_page, ScriptInterface* srcScriptInterface, JS::HandleValue initData) { { // console TIMER(L"ps_console"); g_Console->Init(); } // hotkeys { TIMER(L"ps_lang_hotkeys"); LoadHotkeys(g_ConfigDB); } if (!setup_gui) { // We do actually need *some* kind of GUI loaded, so use the // (currently empty) Atlas one g_GUI->SwitchPage(L"page_atlas.xml", srcScriptInterface, initData); return; } // GUI uses VFS, so this must come after VFS init. g_GUI->SwitchPage(gui_page, srcScriptInterface, initData); } void InitPsAutostart(bool networked, JS::HandleValue attrs) { // The GUI has not been initialized yet, so use the simulation scriptinterface for this variable ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); ScriptRequest rq(scriptInterface); JS::RootedValue playerAssignments(rq.cx); Script::CreateObject(rq, &playerAssignments); if (!networked) { JS::RootedValue localPlayer(rq.cx); Script::CreateObject(rq, &localPlayer, "player", g_Game->GetPlayerID()); Script::SetProperty(rq, playerAssignments, "local", localPlayer); } JS::RootedValue sessionInitData(rq.cx); Script::CreateObject( rq, &sessionInitData, "attribs", attrs, "playerAssignments", playerAssignments); InitPs(true, L"page_loading.xml", &scriptInterface, sessionInitData); } void InitInput() { g_Joystick.Initialise(); // register input handlers // This stack is constructed so the first added, will be the last // one called. This is important, because each of the handlers // has the potential to block events to go further down // in the chain. I.e. the last one in the list added, is the // only handler that can block all messages before they are // processed. in_add_handler(game_view_handler); in_add_handler(CProfileViewer::InputThunk); in_add_handler(HotkeyInputActualHandler); // gui_handler needs to be registered after (i.e. called before!) the // hotkey handler so that input boxes can be typed in without // setting off hotkeys. in_add_handler(gui_handler); // Likewise for the console. in_add_handler(conInputHandler); in_add_handler(touch_input_handler); // Should be called after scancode map update (i.e. after the global input, but before UI). // This never blocks the event, but it does some processing necessary for hotkeys, // which are triggered later down the input chain. // (by calling this before the UI, we can use 'EventWouldTriggerHotkey' in the UI). in_add_handler(HotkeyInputPrepHandler); // These two must be called first (i.e. pushed last) // GlobalsInputHandler deals with some important global state, // such as which scancodes are being pressed, mouse buttons pressed, etc. // while HotkeyStateChange updates the map of active hotkeys. in_add_handler(GlobalsInputHandler); in_add_handler(HotkeyStateChange); } static void ShutdownPs() { SAFE_DELETE(g_GUI); UnloadHotkeys(); } static void InitSDL() { #if OS_LINUX // In fullscreen mode when SDL is compiled with DGA support, the mouse // sensitivity often appears to be unusably wrong (typically too low). // (This seems to be reported almost exclusively on Ubuntu, but can be // reproduced on Gentoo after explicitly enabling DGA.) // Disabling the DGA mouse appears to fix that problem, and doesn't // have any obvious negative effects. setenv("SDL_VIDEO_X11_DGAMOUSE", "0", 0); #endif if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_NOPARACHUTE) < 0) { LOGERROR("SDL library initialization failed: %s", SDL_GetError()); throw PSERROR_System_SDLInitFailed(); } atexit(SDL_Quit); // Text input is active by default, disable it until it is actually needed. SDL_StopTextInput(); #if SDL_VERSION_ATLEAST(2, 0, 9) // SDL2 >= 2.0.9 defaults to 32 pixels (to support touch screens) but that can break our double-clicking. SDL_SetHint(SDL_HINT_MOUSE_DOUBLE_CLICK_RADIUS, "1"); #endif #if SDL_VERSION_ATLEAST(2, 0, 14) && OS_WIN // SDL2 >= 2.0.14 Before SDL 2.0.14, this defaulted to true. In 2.0.14 they switched to false // breaking the behavior on Windows. // https://github.com/libsdl-org/SDL/commit/1947ca7028ab165cc3e6cbdb0b4b7c4db68d1710 // https://github.com/libsdl-org/SDL/issues/5033 SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "1"); #endif #if OS_MACOSX // Some Mac mice only have one button, so they can't right-click // but SDL2 can emulate that with Ctrl+Click bool macMouse = false; CFG_GET_VAL("macmouse", macMouse); SDL_SetHint(SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK, macMouse ? "1" : "0"); #endif } static void ShutdownSDL() { SDL_Quit(); } void EndGame() { SAFE_DELETE(g_NetClient); SAFE_DELETE(g_NetServer); SAFE_DELETE(g_Game); if (CRenderer::IsInitialised()) { ISoundManager::CloseGame(); g_Renderer.GetSceneRenderer().ResetState(); } } void Shutdown(int flags) { const bool hasRenderer = CRenderer::IsInitialised(); if ((flags & SHUTDOWN_FROM_CONFIG)) goto from_config; EndGame(); SAFE_DELETE(g_XmppClient); SAFE_DELETE(g_ModIo); ShutdownPs(); if (hasRenderer) { TIMER_BEGIN(L"shutdown Renderer"); g_Renderer.~CRenderer(); g_VBMan.Shutdown(); TIMER_END(L"shutdown Renderer"); } g_RenderingOptions.ClearHooks(); g_Profiler2.ShutdownGPU(); TIMER_BEGIN(L"shutdown SDL"); ShutdownSDL(); TIMER_END(L"shutdown SDL"); if (hasRenderer) g_VideoMode.Shutdown(); TIMER_BEGIN(L"shutdown UserReporter"); g_UserReporter.Deinitialize(); TIMER_END(L"shutdown UserReporter"); // Cleanup curl now that g_ModIo and g_UserReporter have been shutdown. curl_global_cleanup(); delete &g_L10n; from_config: TIMER_BEGIN(L"shutdown ConfigDB"); CConfigDB::Shutdown(); TIMER_END(L"shutdown ConfigDB"); SAFE_DELETE(g_Console); // This is needed to ensure that no callbacks from the JSAPI try to use // the profiler when it's already destructed g_ScriptContext.reset(); // resource // first shut down all resource owners, and then the handle manager. TIMER_BEGIN(L"resource modules"); ISoundManager::SetEnabled(false); g_VFS.reset(); - // this forcibly frees all open handles (thus preventing real leaks), - // and makes further access to h_mgr impossible. - h_mgr_shutdown(); - file_stats_dump(); TIMER_END(L"resource modules"); TIMER_BEGIN(L"shutdown misc"); timer_DisplayClientTotals(); CNetHost::Deinitialize(); // should be last, since the above use them SAFE_DELETE(g_Logger); delete &g_Profiler; delete &g_ProfileViewer; SAFE_DELETE(g_ScriptStatsTable); TIMER_END(L"shutdown misc"); } #if OS_UNIX static void FixLocales() { #if OS_MACOSX || OS_BSD // OS X requires a UTF-8 locale in LC_CTYPE so that *wprintf can handle // wide characters. Peculiarly the string "UTF-8" seems to be acceptable // despite not being a real locale, and it's conveniently language-agnostic, // so use that. setlocale(LC_CTYPE, "UTF-8"); #endif // On misconfigured systems with incorrect locale settings, we'll die // with a C++ exception when some code (e.g. Boost) tries to use locales. // To avoid death, we'll detect the problem here and warn the user and // reset to the default C locale. // For informing the user of the problem, use the list of env vars that // glibc setlocale looks at. (LC_ALL is checked first, and LANG last.) const char* const LocaleEnvVars[] = { "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", "LANG" }; try { // this constructor is similar to setlocale(LC_ALL, ""), // but instead of returning NULL, it throws runtime_error // when the first locale env variable found contains an invalid value std::locale(""); } catch (std::runtime_error&) { LOGWARNING("Invalid locale settings"); for (size_t i = 0; i < ARRAY_SIZE(LocaleEnvVars); i++) { if (char* envval = getenv(LocaleEnvVars[i])) LOGWARNING(" %s=\"%s\"", LocaleEnvVars[i], envval); else LOGWARNING(" %s=\"(unset)\"", LocaleEnvVars[i]); } // We should set LC_ALL since it overrides LANG if (setenv("LC_ALL", std::locale::classic().name().c_str(), 1)) debug_warn(L"Invalid locale settings, and unable to set LC_ALL env variable."); else LOGWARNING("Setting LC_ALL env variable to: %s", getenv("LC_ALL")); } } #else static void FixLocales() { // Do nothing on Windows } #endif void EarlyInit() { // If you ever want to catch a particular allocation: //_CrtSetBreakAlloc(232647); Threading::SetMainThread(); debug_SetThreadName("main"); // add all debug_printf "tags" that we are interested in: debug_filter_add("TIMER"); timer_Init(); // initialise profiler early so it can profile startup, // but only after LatchStartTime g_Profiler2.Initialise(); FixLocales(); // Because we do GL calls from a secondary thread, Xlib needs to // be told to support multiple threads safely. // This is needed for Atlas, but we have to call it before any other // Xlib functions (e.g. the ones used when drawing the main menu // before launching Atlas) #if MUST_INIT_X11 int status = XInitThreads(); if (status == 0) debug_printf("Error enabling thread-safety via XInitThreads\n"); #endif // Initialise the low-quality rand function srand(time(NULL)); // NOTE: this rand should *not* be used for simulation! } bool Autostart(const CmdLineArgs& args); /** * Returns true if the user has intended to start a visual replay from command line. */ bool AutostartVisualReplay(const std::string& replayFile); bool Init(const CmdLineArgs& args, int flags) { - h_mgr_init(); - // Do this as soon as possible, because it chdirs // and will mess up the error reporting if anything // crashes before the working directory is set. InitVfs(args, flags); // This must come after VFS init, which sets the current directory // (required for finding our output log files). g_Logger = new CLogger; new CProfileViewer; new CProfileManager; // before any script code g_ScriptStatsTable = new CScriptStatsTable; g_ProfileViewer.AddRootTable(g_ScriptStatsTable); // Set up the console early, so that debugging // messages can be logged to it. (The console's size // and fonts are set later in InitPs()) g_Console = new CConsole(); // g_ConfigDB, command line args, globals CONFIG_Init(args); // Using a global object for the context is a workaround until Simulation and AI use // their own threads and also their own contexts. const int contextSize = 384 * 1024 * 1024; const int heapGrowthBytesGCTrigger = 20 * 1024 * 1024; g_ScriptContext = ScriptContext::CreateContext(contextSize, heapGrowthBytesGCTrigger); // On the first Init (INIT_MODS), check for command-line arguments // or use the default mods from the config and enable those. // On later engine restarts (e.g. the mod selector), we will skip this path, // to avoid overwriting the newly selected mods. if (flags & INIT_MODS) { ScriptInterface modInterface("Engine", "Mod", g_ScriptContext); g_Mods.UpdateAvailableMods(modInterface); std::vector mods; if (args.Has("mod")) mods = args.GetMultiple("mod"); else { CStr modsStr; CFG_GET_VAL("mod.enabledmods", modsStr); boost::split(mods, modsStr, boost::algorithm::is_space(), boost::token_compress_on); } if (!g_Mods.EnableMods(mods, flags & INIT_MODS_PUBLIC)) { // In non-visual mode, fail entirely. if (args.Has("autostart-nonvisual")) { LOGERROR("Trying to start with incompatible mods: %s.", boost::algorithm::join(g_Mods.GetIncompatibleMods(), ", ")); return false; } } } // If there are incompatible mods, switch to the mod selector so players can resolve the problem. if (g_Mods.GetIncompatibleMods().empty()) MountMods(Paths(args), g_Mods.GetEnabledMods()); else MountMods(Paths(args), { "mod" }); // Special command-line mode to dump the entity schemas instead of running the game. // (This must be done after loading VFS etc, but should be done before wasting time // on anything else.) if (args.Has("dumpSchema")) { CSimulation2 sim(NULL, g_ScriptContext, NULL); sim.LoadDefaultScripts(); std::ofstream f("entity.rng", std::ios_base::out | std::ios_base::trunc); f << sim.GenerateSchema(); std::cout << "Generated entity.rng\n"; exit(0); } CNetHost::Initialize(); #if CONFIG2_AUDIO if (!args.Has("autostart-nonvisual") && !g_DisableAudio) ISoundManager::CreateSoundManager(); #endif new L10n; // Optionally start profiler HTTP output automatically // (By default it's only enabled by a hotkey, for security/performance) bool profilerHTTPEnable = false; CFG_GET_VAL("profiler2.autoenable", profilerHTTPEnable); if (profilerHTTPEnable) g_Profiler2.EnableHTTP(); // Initialise everything except Win32 sockets (because our networking // system already inits those) curl_global_init(CURL_GLOBAL_ALL & ~CURL_GLOBAL_WIN32); if (!g_Quickstart) g_UserReporter.Initialize(); // after config PROFILE2_EVENT("Init finished"); return true; } void InitGraphics(const CmdLineArgs& args, int flags, const std::vector& installedMods) { const bool setup_vmode = (flags & INIT_HAVE_VMODE) == 0; if(setup_vmode) { InitSDL(); if (!g_VideoMode.InitSDL()) throw PSERROR_System_VmodeFailed(); // abort startup } RunHardwareDetection(); ogl_WarnIfError(); // Optionally start profiler GPU timings automatically // (By default it's only enabled by a hotkey, for performance/compatibility) bool profilerGPUEnable = false; CFG_GET_VAL("profiler2.autoenable", profilerGPUEnable); if (profilerGPUEnable) g_Profiler2.EnableGPU(); if(!g_Quickstart) { WriteSystemInfo(); // note: no longer vfs_display here. it's dog-slow due to unbuffered // file output and very rarely needed. } if(g_DisableAudio) ISoundManager::SetEnabled(false); g_GUI = new CGUIManager(); CStr8 renderPath = "default"; CFG_GET_VAL("renderpath", renderPath); if (RenderPathEnum::FromString(renderPath) == FIXED) { // It doesn't make sense to continue working here, because we're not // able to display anything. DEBUG_DISPLAY_FATAL_ERROR( L"Your graphics card doesn't appear to be fully compatible with OpenGL shaders." L" The game does not support pre-shader graphics cards." L" You are advised to try installing newer drivers and/or upgrade your graphics card." L" For more information, please see http://www.wildfiregames.com/forum/index.php?showtopic=16734" ); } ogl_WarnIfError(); g_RenderingOptions.ReadConfigAndSetupHooks(); // create renderer new CRenderer; InitInput(); ogl_WarnIfError(); // TODO: Is this the best place for this? if (VfsDirectoryExists(L"maps/")) CXeromyces::AddValidator(g_VFS, "map", "maps/scenario.rng"); try { if (!AutostartVisualReplay(args.Get("replay-visual")) && !Autostart(args)) { const bool setup_gui = ((flags & INIT_NO_GUI) == 0); // We only want to display the splash screen at startup std::shared_ptr scriptInterface = g_GUI->GetScriptInterface(); ScriptRequest rq(scriptInterface); JS::RootedValue data(rq.cx); if (g_GUI) { Script::CreateObject(rq, &data, "isStartup", true); if (!installedMods.empty()) Script::SetProperty(rq, data, "installedMods", installedMods); } InitPs(setup_gui, installedMods.empty() ? L"page_pregame.xml" : L"page_modmod.xml", g_GUI->GetScriptInterface().get(), data); } } catch (PSERROR_Game_World_MapLoadFailed& e) { // Map Loading failed // Start the engine so we have a GUI InitPs(true, L"page_pregame.xml", NULL, JS::UndefinedHandleValue); // Call script function to do the actual work // (delete game data, switch GUI page, show error, etc.) CancelLoad(CStr(e.what()).FromUTF8()); } } void InitNonVisual(const CmdLineArgs& args) { Autostart(args); } /** * Temporarily loads a scenario map and retrieves the "ScriptSettings" JSON * data from it. * The scenario map format is used for scenario and skirmish map types (random * games do not use a "map" (format) but a small JavaScript program which * creates a map on the fly). It contains a section to initialize the game * setup screen. * @param mapPath Absolute path (from VFS root) to the map file to peek in. * @return ScriptSettings in JSON format extracted from the map. */ CStr8 LoadSettingsOfScenarioMap(const VfsPath &mapPath) { CXeromyces mapFile; const char *pathToSettings[] = { "Scenario", "ScriptSettings", "" // Path to JSON data in map }; Status loadResult = mapFile.Load(g_VFS, mapPath); if (INFO::OK != loadResult) { LOGERROR("LoadSettingsOfScenarioMap: Unable to load map file '%s'", mapPath.string8()); throw PSERROR_Game_World_MapLoadFailed("Unable to load map file, check the path for typos."); } XMBElement mapElement = mapFile.GetRoot(); // Select the ScriptSettings node in the map file... for (int i = 0; pathToSettings[i][0]; ++i) { int childId = mapFile.GetElementID(pathToSettings[i]); XMBElementList nodes = mapElement.GetChildNodes(); auto it = std::find_if(nodes.begin(), nodes.end(), [&childId](const XMBElement& child) { return child.GetNodeName() == childId; }); if (it != nodes.end()) mapElement = *it; } // ... they contain a JSON document to initialize the game setup // screen return mapElement.GetText(); } /* * Command line options for autostart * (keep synchronized with binaries/system/readme.txt): * * -autostart="TYPEDIR/MAPNAME" enables autostart and sets MAPNAME; * TYPEDIR is skirmishes, scenarios, or random * -autostart-seed=SEED sets randomization seed value (default 0, use -1 for random) * -autostart-ai=PLAYER:AI sets the AI for PLAYER (e.g. 2:petra) * -autostart-aidiff=PLAYER:DIFF sets the DIFFiculty of PLAYER's AI * (0: sandbox, 5: very hard) * -autostart-aiseed=AISEED sets the seed used for the AI random * generator (default 0, use -1 for random) * -autostart-player=NUMBER sets the playerID in non-networked games (default 1, use -1 for observer) * -autostart-civ=PLAYER:CIV sets PLAYER's civilisation to CIV * (skirmish and random maps only) * -autostart-team=PLAYER:TEAM sets the team for PLAYER (e.g. 2:2). * -autostart-ceasefire=NUM sets a ceasefire duration NUM * (default 0 minutes) * -autostart-nonvisual disable any graphics and sounds * -autostart-victory=SCRIPTNAME sets the victory conditions with SCRIPTNAME * located in simulation/data/settings/victory_conditions/ * (default conquest). When the first given SCRIPTNAME is * "endless", no victory conditions will apply. * -autostart-wonderduration=NUM sets the victory duration NUM for wonder victory condition * (default 10 minutes) * -autostart-relicduration=NUM sets the victory duration NUM for relic victory condition * (default 10 minutes) * -autostart-reliccount=NUM sets the number of relics for relic victory condition * (default 2 relics) * -autostart-disable-replay disable saving of replays * * Multiplayer: * -autostart-playername=NAME sets local player NAME (default 'anonymous') * -autostart-host sets multiplayer host mode * -autostart-host-players=NUMBER sets NUMBER of human players for multiplayer * game (default 2) * -autostart-client=IP sets multiplayer client to join host at * given IP address * Random maps only: * -autostart-size=TILES sets random map size in TILES (default 192) * -autostart-players=NUMBER sets NUMBER of players on random map * (default 2) * * Examples: * 1) "Bob" will host a 2 player game on the Arcadia map: * -autostart="scenarios/Arcadia" -autostart-host -autostart-host-players=2 -autostart-playername="Bob" * "Alice" joins the match as player 2: * -autostart="scenarios/Arcadia" -autostart-client=127.0.0.1 -autostart-playername="Alice" * The players use the developer overlay to control players. * * 2) Load Alpine Lakes random map with random seed, 2 players (Athens and Britons), and player 2 is PetraBot: * -autostart="random/alpine_lakes" -autostart-seed=-1 -autostart-players=2 -autostart-civ=1:athen -autostart-civ=2:brit -autostart-ai=2:petra * * 3) Observe the PetraBot on a triggerscript map: * -autostart="random/jebel_barkal" -autostart-seed=-1 -autostart-players=2 -autostart-civ=1:athen -autostart-civ=2:brit -autostart-ai=1:petra -autostart-ai=2:petra -autostart-player=-1 */ bool Autostart(const CmdLineArgs& args) { CStr autoStartName = args.Get("autostart"); if (autoStartName.empty()) return false; g_Game = new CGame(!args.Has("autostart-disable-replay")); ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); ScriptRequest rq(scriptInterface); JS::RootedValue attrs(rq.cx); JS::RootedValue settings(rq.cx); JS::RootedValue playerData(rq.cx); Script::CreateObject(rq, &attrs); Script::CreateObject(rq, &settings); Script::CreateArray(rq, &playerData); // The directory in front of the actual map name indicates which type // of map is being loaded. Drawback of this approach is the association // of map types and folders is hard-coded, but benefits are: // - No need to pass the map type via command line separately // - Prevents mixing up of scenarios and skirmish maps to some degree Path mapPath = Path(autoStartName); std::wstring mapDirectory = mapPath.Parent().Filename().string(); std::string mapType; if (mapDirectory == L"random") { // Random map definition will be loaded from JSON file, so we need to parse it std::wstring scriptPath = L"maps/" + autoStartName.FromUTF8() + L".json"; JS::RootedValue scriptData(rq.cx); Script::ReadJSONFile(rq, scriptPath, &scriptData); if (!scriptData.isUndefined() && Script::GetProperty(rq, scriptData, "settings", &settings)) { // JSON loaded ok - copy script name over to game attributes std::wstring scriptFile; if (!Script::GetProperty(rq, settings, "Script", scriptFile)) { LOGERROR("Autostart: random map '%s' data has no 'Script' property.", utf8_from_wstring(scriptPath)); throw PSERROR_Game_World_MapLoadFailed("Error reading random map script.\nCheck application log for details."); } Script::SetProperty(rq, attrs, "script", scriptFile); // RMS filename } else { // Problem with JSON file LOGERROR("Autostart: Error reading random map script '%s'", utf8_from_wstring(scriptPath)); throw PSERROR_Game_World_MapLoadFailed("Error reading random map script.\nCheck application log for details."); } // Get optional map size argument (default 192) uint mapSize = 192; if (args.Has("autostart-size")) { CStr size = args.Get("autostart-size"); mapSize = size.ToUInt(); } Script::SetProperty(rq, settings, "Size", mapSize); // Random map size (in patches) // Get optional number of players (default 2) size_t numPlayers = 2; if (args.Has("autostart-players")) { CStr num = args.Get("autostart-players"); numPlayers = num.ToUInt(); } // Set up player data for (size_t i = 0; i < numPlayers; ++i) { JS::RootedValue player(rq.cx); // We could load player_defaults.json here, but that would complicate the logic // even more and autostart is only intended for developers anyway Script::CreateObject(rq, &player, "Civ", "athen"); Script::SetPropertyInt(rq, playerData, i, player); } mapType = "random"; } else if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") { // Initialize general settings from the map data so some values // (e.g. name of map) are always present, even when autostart is // partially configured CStr8 mapSettingsJSON = LoadSettingsOfScenarioMap("maps/" + autoStartName + ".xml"); Script::ParseJSON(rq, mapSettingsJSON, &settings); // Initialize the playerData array being modified by autostart // with the real map data, so sensible values are present: Script::GetProperty(rq, settings, "PlayerData", &playerData); if (mapDirectory == L"scenarios") mapType = "scenario"; else mapType = "skirmish"; } else { LOGERROR("Autostart: Unrecognized map type '%s'", utf8_from_wstring(mapDirectory)); throw PSERROR_Game_World_MapLoadFailed("Unrecognized map type.\nConsult readme.txt for the currently supported types."); } Script::SetProperty(rq, attrs, "mapType", mapType); Script::SetProperty(rq, attrs, "map", "maps/" + autoStartName); Script::SetProperty(rq, settings, "mapType", mapType); Script::SetProperty(rq, settings, "CheatsEnabled", true); // The seed is used for both random map generation and simulation u32 seed = 0; if (args.Has("autostart-seed")) { CStr seedArg = args.Get("autostart-seed"); if (seedArg == "-1") seed = rand(); else seed = seedArg.ToULong(); } Script::SetProperty(rq, settings, "Seed", seed); // Set seed for AIs u32 aiseed = 0; if (args.Has("autostart-aiseed")) { CStr seedArg = args.Get("autostart-aiseed"); if (seedArg == "-1") aiseed = rand(); else aiseed = seedArg.ToULong(); } Script::SetProperty(rq, settings, "AISeed", aiseed); // Set player data for AIs // attrs.settings = { PlayerData: [ { AI: ... }, ... ] } // or = { PlayerData: [ null, { AI: ... }, ... ] } when gaia set int offset = 1; JS::RootedValue player(rq.cx); if (Script::GetPropertyInt(rq, playerData, 0, &player) && player.isNull()) offset = 0; // Set teams if (args.Has("autostart-team")) { std::vector civArgs = args.GetMultiple("autostart-team"); for (size_t i = 0; i < civArgs.size(); ++i) { int playerID = civArgs[i].BeforeFirst(":").ToInt(); // Instead of overwriting existing player data, modify the array JS::RootedValue currentPlayer(rq.cx); if (!Script::GetPropertyInt(rq, playerData, playerID-offset, ¤tPlayer) || currentPlayer.isUndefined()) { if (mapDirectory == L"skirmishes") { // playerID is certainly bigger than this map player number LOGWARNING("Autostart: Invalid player %d in autostart-team option", playerID); continue; } Script::CreateObject(rq, ¤tPlayer); } int teamID = civArgs[i].AfterFirst(":").ToInt() - 1; Script::SetProperty(rq, currentPlayer, "Team", teamID); Script::SetPropertyInt(rq, playerData, playerID-offset, currentPlayer); } } int ceasefire = 0; if (args.Has("autostart-ceasefire")) ceasefire = args.Get("autostart-ceasefire").ToInt(); Script::SetProperty(rq, settings, "Ceasefire", ceasefire); if (args.Has("autostart-ai")) { std::vector aiArgs = args.GetMultiple("autostart-ai"); for (size_t i = 0; i < aiArgs.size(); ++i) { int playerID = aiArgs[i].BeforeFirst(":").ToInt(); // Instead of overwriting existing player data, modify the array JS::RootedValue currentPlayer(rq.cx); if (!Script::GetPropertyInt(rq, playerData, playerID-offset, ¤tPlayer) || currentPlayer.isUndefined()) { if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") { // playerID is certainly bigger than this map player number LOGWARNING("Autostart: Invalid player %d in autostart-ai option", playerID); continue; } Script::CreateObject(rq, ¤tPlayer); } Script::SetProperty(rq, currentPlayer, "AI", aiArgs[i].AfterFirst(":")); Script::SetProperty(rq, currentPlayer, "AIDiff", 3); Script::SetProperty(rq, currentPlayer, "AIBehavior", "balanced"); Script::SetPropertyInt(rq, playerData, playerID-offset, currentPlayer); } } // Set AI difficulty if (args.Has("autostart-aidiff")) { std::vector civArgs = args.GetMultiple("autostart-aidiff"); for (size_t i = 0; i < civArgs.size(); ++i) { int playerID = civArgs[i].BeforeFirst(":").ToInt(); // Instead of overwriting existing player data, modify the array JS::RootedValue currentPlayer(rq.cx); if (!Script::GetPropertyInt(rq, playerData, playerID-offset, ¤tPlayer) || currentPlayer.isUndefined()) { if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") { // playerID is certainly bigger than this map player number LOGWARNING("Autostart: Invalid player %d in autostart-aidiff option", playerID); continue; } Script::CreateObject(rq, ¤tPlayer); } Script::SetProperty(rq, currentPlayer, "AIDiff", civArgs[i].AfterFirst(":").ToInt()); Script::SetPropertyInt(rq, playerData, playerID-offset, currentPlayer); } } // Set player data for Civs if (args.Has("autostart-civ")) { if (mapDirectory != L"scenarios") { std::vector civArgs = args.GetMultiple("autostart-civ"); for (size_t i = 0; i < civArgs.size(); ++i) { int playerID = civArgs[i].BeforeFirst(":").ToInt(); // Instead of overwriting existing player data, modify the array JS::RootedValue currentPlayer(rq.cx); if (!Script::GetPropertyInt(rq, playerData, playerID-offset, ¤tPlayer) || currentPlayer.isUndefined()) { if (mapDirectory == L"skirmishes") { // playerID is certainly bigger than this map player number LOGWARNING("Autostart: Invalid player %d in autostart-civ option", playerID); continue; } Script::CreateObject(rq, ¤tPlayer); } Script::SetProperty(rq, currentPlayer, "Civ", civArgs[i].AfterFirst(":")); Script::SetPropertyInt(rq, playerData, playerID-offset, currentPlayer); } } else LOGWARNING("Autostart: Option 'autostart-civ' is invalid for scenarios"); } // Add player data to map settings Script::SetProperty(rq, settings, "PlayerData", playerData); // Add map settings to game attributes Script::SetProperty(rq, attrs, "settings", settings); // Get optional playername CStrW userName = L"anonymous"; if (args.Has("autostart-playername")) userName = args.Get("autostart-playername").FromUTF8(); // Add additional scripts to the TriggerScripts property std::vector triggerScriptsVector; JS::RootedValue triggerScripts(rq.cx); if (Script::HasProperty(rq, settings, "TriggerScripts")) { Script::GetProperty(rq, settings, "TriggerScripts", &triggerScripts); Script::FromJSVal(rq, triggerScripts, triggerScriptsVector); } if (!CRenderer::IsInitialised()) { CStr nonVisualScript = "scripts/NonVisualTrigger.js"; triggerScriptsVector.push_back(nonVisualScript.FromUTF8()); } std::vector victoryConditions(1, "conquest"); if (args.Has("autostart-victory")) victoryConditions = args.GetMultiple("autostart-victory"); if (victoryConditions.size() == 1 && victoryConditions[0] == "endless") victoryConditions.clear(); Script::SetProperty(rq, settings, "VictoryConditions", victoryConditions); for (const CStr& victory : victoryConditions) { JS::RootedValue scriptData(rq.cx); JS::RootedValue data(rq.cx); JS::RootedValue victoryScripts(rq.cx); CStrW scriptPath = L"simulation/data/settings/victory_conditions/" + victory.FromUTF8() + L".json"; Script::ReadJSONFile(rq, scriptPath, &scriptData); if (!scriptData.isUndefined() && Script::GetProperty(rq, scriptData, "Data", &data) && !data.isUndefined() && Script::GetProperty(rq, data, "Scripts", &victoryScripts) && !victoryScripts.isUndefined()) { std::vector victoryScriptsVector; Script::FromJSVal(rq, victoryScripts, victoryScriptsVector); triggerScriptsVector.insert(triggerScriptsVector.end(), victoryScriptsVector.begin(), victoryScriptsVector.end()); } else { LOGERROR("Autostart: Error reading victory script '%s'", utf8_from_wstring(scriptPath)); throw PSERROR_Game_World_MapLoadFailed("Error reading victory script.\nCheck application log for details."); } } Script::ToJSVal(rq, &triggerScripts, triggerScriptsVector); Script::SetProperty(rq, settings, "TriggerScripts", triggerScripts); int wonderDuration = 10; if (args.Has("autostart-wonderduration")) wonderDuration = args.Get("autostart-wonderduration").ToInt(); Script::SetProperty(rq, settings, "WonderDuration", wonderDuration); int relicDuration = 10; if (args.Has("autostart-relicduration")) relicDuration = args.Get("autostart-relicduration").ToInt(); Script::SetProperty(rq, settings, "RelicDuration", relicDuration); int relicCount = 2; if (args.Has("autostart-reliccount")) relicCount = args.Get("autostart-reliccount").ToInt(); Script::SetProperty(rq, settings, "RelicCount", relicCount); if (args.Has("autostart-host")) { InitPsAutostart(true, attrs); size_t maxPlayers = 2; if (args.Has("autostart-host-players")) maxPlayers = args.Get("autostart-host-players").ToUInt(); // Generate a secret to identify the host client. std::string secret = ps_generate_guid(); g_NetServer = new CNetServer(false, maxPlayers); g_NetServer->SetControllerSecret(secret); g_NetServer->UpdateInitAttributes(&attrs, scriptInterface); bool ok = g_NetServer->SetupConnection(PS_DEFAULT_PORT); ENSURE(ok); g_NetClient = new CNetClient(g_Game); g_NetClient->SetUserName(userName); g_NetClient->SetupServerData("127.0.0.1", PS_DEFAULT_PORT, false); g_NetClient->SetControllerSecret(secret); g_NetClient->SetupConnection(nullptr); } else if (args.Has("autostart-client")) { InitPsAutostart(true, attrs); g_NetClient = new CNetClient(g_Game); g_NetClient->SetUserName(userName); CStr ip = args.Get("autostart-client"); if (ip.empty()) ip = "127.0.0.1"; g_NetClient->SetupServerData(ip, PS_DEFAULT_PORT, false); ENSURE(g_NetClient->SetupConnection(nullptr)); } else { g_Game->SetPlayerID(args.Has("autostart-player") ? args.Get("autostart-player").ToInt() : 1); g_Game->StartGame(&attrs, ""); if (CRenderer::IsInitialised()) { InitPsAutostart(false, attrs); } else { // TODO: Non progressive load can fail - need a decent way to handle this LDR_NonprogressiveLoad(); ENSURE(g_Game->ReallyStartGame() == PSRETURN_OK); } } return true; } bool AutostartVisualReplay(const std::string& replayFile) { if (!FileExists(OsPath(replayFile))) return false; g_Game = new CGame(false); g_Game->SetPlayerID(-1); g_Game->StartVisualReplay(replayFile); ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); ScriptRequest rq(scriptInterface); JS::RootedValue attrs(rq.cx, g_Game->GetSimulation2()->GetInitAttributes()); InitPsAutostart(false, attrs); return true; } void CancelLoad(const CStrW& message) { std::shared_ptr pScriptInterface = g_GUI->GetActiveGUI()->GetScriptInterface(); ScriptRequest rq(pScriptInterface); JS::RootedValue global(rq.cx, rq.globalValue()); LDR_Cancel(); if (g_GUI && g_GUI->GetPageCount() && Script::HasProperty(rq, global, "cancelOnLoadGameError")) ScriptFunction::CallVoid(rq, global, "cancelOnLoadGameError", message); } bool InDevelopmentCopy() { if (!g_CheckedIfInDevelopmentCopy) { g_InDevelopmentCopy = (g_VFS->GetFileInfo(L"config/dev.cfg", NULL) == INFO::OK); g_CheckedIfInDevelopmentCopy = true; } return g_InDevelopmentCopy; } Index: ps/trunk/source/ps/Replay.cpp =================================================================== --- ps/trunk/source/ps/Replay.cpp (revision 26368) +++ ps/trunk/source/ps/Replay.cpp (revision 26369) @@ -1,347 +1,343 @@ /* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "Replay.h" #include "graphics/TerrainTextureManager.h" #include "lib/timer.h" #include "lib/file/file_system.h" -#include "lib/res/h_mgr.h" #include "lib/tex/tex.h" #include "ps/CLogger.h" #include "ps/Game.h" #include "ps/GameSetup/GameSetup.h" #include "ps/GameSetup/CmdLineArgs.h" #include "ps/GameSetup/Paths.h" #include "ps/Loader.h" #include "ps/Mod.h" #include "ps/Profile.h" #include "ps/ProfileViewer.h" #include "ps/Pyrogenesis.h" #include "ps/Mod.h" #include "ps/Util.h" #include "ps/VisualReplay.h" #include "scriptinterface/FunctionWrapper.h" #include "scriptinterface/Object.h" #include "scriptinterface/ScriptContext.h" #include "scriptinterface/ScriptInterface.h" #include "scriptinterface/ScriptRequest.h" #include "scriptinterface/ScriptStats.h" #include "scriptinterface/JSON.h" #include "simulation2/components/ICmpGuiInterface.h" #include "simulation2/helpers/Player.h" #include "simulation2/helpers/SimulationCommand.h" #include "simulation2/Simulation2.h" #include "simulation2/system/CmpPtr.h" #include #include /** * Number of turns between two saved profiler snapshots. * Keep in sync with source/tools/replayprofile/graph.js */ static const int PROFILE_TURN_INTERVAL = 20; CReplayLogger::CReplayLogger(const ScriptInterface& scriptInterface) : m_ScriptInterface(scriptInterface), m_Stream(NULL) { } CReplayLogger::~CReplayLogger() { delete m_Stream; } void CReplayLogger::StartGame(JS::MutableHandleValue attribs) { ScriptRequest rq(m_ScriptInterface); // Add timestamp, since the file-modification-date can change Script::SetProperty(rq, attribs, "timestamp", (double)std::time(nullptr)); // Add engine version and currently loaded mods for sanity checks when replaying Script::SetProperty(rq, attribs, "engine_version", engine_version); JS::RootedValue mods(rq.cx); Script::ToJSVal(rq, &mods, g_Mods.GetEnabledModsData()); Script::SetProperty(rq, attribs, "mods", mods); m_Directory = createDateIndexSubdirectory(VisualReplay::GetDirectoryPath()); debug_printf("Writing replay to %s\n", m_Directory.string8().c_str()); m_Stream = new std::ofstream(OsString(m_Directory / L"commands.txt").c_str(), std::ofstream::out | std::ofstream::trunc); *m_Stream << "start " << Script::StringifyJSON(rq, attribs, false) << "\n"; } void CReplayLogger::Turn(u32 n, u32 turnLength, std::vector& commands) { ScriptRequest rq(m_ScriptInterface); *m_Stream << "turn " << n << " " << turnLength << "\n"; for (SimulationCommand& command : commands) *m_Stream << "cmd " << command.player << " " << Script::StringifyJSON(rq, &command.data, false) << "\n"; *m_Stream << "end\n"; m_Stream->flush(); } void CReplayLogger::Hash(const std::string& hash, bool quick) { if (quick) *m_Stream << "hash-quick " << Hexify(hash) << "\n"; else *m_Stream << "hash " << Hexify(hash) << "\n"; } void CReplayLogger::SaveMetadata(const CSimulation2& simulation) { CmpPtr cmpGuiInterface(simulation, SYSTEM_ENTITY); if (!cmpGuiInterface) { LOGERROR("Could not save replay metadata!"); return; } ScriptInterface& scriptInterface = simulation.GetScriptInterface(); ScriptRequest rq(scriptInterface); JS::RootedValue arg(rq.cx); JS::RootedValue metadata(rq.cx); cmpGuiInterface->ScriptCall(INVALID_PLAYER, L"GetReplayMetadata", arg, &metadata); const OsPath fileName = g_Game->GetReplayLogger().GetDirectory() / L"metadata.json"; CreateDirectories(fileName.Parent(), 0700); std::ofstream stream (OsString(fileName).c_str(), std::ofstream::out | std::ofstream::trunc); stream << Script::StringifyJSON(rq, &metadata, false); stream.close(); debug_printf("Saved replay metadata to %s\n", fileName.string8().c_str()); } OsPath CReplayLogger::GetDirectory() const { return m_Directory; } //////////////////////////////////////////////////////////////// CReplayPlayer::CReplayPlayer() : m_Stream(NULL) { } CReplayPlayer::~CReplayPlayer() { delete m_Stream; } void CReplayPlayer::Load(const OsPath& path) { ENSURE(!m_Stream); m_Stream = new std::ifstream(OsString(path).c_str()); ENSURE(m_Stream->good()); } namespace { CStr ModListToString(const std::vector& list) { CStr text; for (const Mod::ModData* data : list) text += data->m_Pathname + " (" + data->m_Version + ")\n"; return text; } void CheckReplayMods(const std::vector& replayMods) { std::vector replayData; replayData.reserve(replayMods.size()); for (const Mod::ModData& data : replayMods) replayData.push_back(&data); if (!Mod::AreModsPlayCompatible(g_Mods.GetEnabledModsData(), replayData)) LOGWARNING("Incompatible replay mods detected.\nThe mods of the replay are:\n%s\nThese mods are enabled:\n%s", ModListToString(replayData), ModListToString(g_Mods.GetEnabledModsData())); } } // anonymous namespace void CReplayPlayer::Replay(const bool serializationtest, const int rejointestturn, const bool ooslog, const bool testHashFull, const bool testHashQuick) { ENSURE(m_Stream); new CProfileViewer; new CProfileManager; g_ScriptStatsTable = new CScriptStatsTable; g_ProfileViewer.AddRootTable(g_ScriptStatsTable); const int contextSize = 384 * 1024 * 1024; const int heapGrowthBytesGCTrigger = 20 * 1024 * 1024; g_ScriptContext = ScriptContext::CreateContext(contextSize, heapGrowthBytesGCTrigger); std::vector commands; u32 turn = 0; u32 turnLength = 0; { std::string type; while ((*m_Stream >> type).good()) { if (type == "start") { std::string attribsStr; { ScriptInterface scriptInterface("Engine", "Replay", g_ScriptContext); ScriptRequest rq(scriptInterface); std::getline(*m_Stream, attribsStr); JS::RootedValue attribs(rq.cx); if (!Script::ParseJSON(rq, attribsStr, &attribs)) { LOGERROR("Error parsing JSON attributes: %s", attribsStr); // TODO: do something cleverer than crashing. ENSURE(false); } // Load the mods specified in the replay. std::vector replayMods; if (!Script::GetProperty(rq, attribs, "mods", replayMods)) { LOGERROR("Could not get replay mod information."); // TODO: do something cleverer than crashing. ENSURE(false); } std::vector mods; for (const Mod::ModData& data : replayMods) mods.emplace_back(data.m_Pathname); // Ignore the return value, we check below. g_Mods.UpdateAvailableMods(scriptInterface); g_Mods.EnableMods(mods, false); CheckReplayMods(replayMods); MountMods(Paths(g_CmdLineArgs), g_Mods.GetEnabledMods()); } g_Game = new CGame(false); if (serializationtest) g_Game->GetSimulation2()->EnableSerializationTest(); if (rejointestturn >= 0) g_Game->GetSimulation2()->EnableRejoinTest(rejointestturn); if (ooslog) g_Game->GetSimulation2()->EnableOOSLog(); - // Initialise h_mgr so it doesn't crash when emitting sounds - h_mgr_init(); - ScriptRequest rq(g_Game->GetSimulation2()->GetScriptInterface()); JS::RootedValue attribs(rq.cx); ENSURE(Script::ParseJSON(rq, attribsStr, &attribs)); g_Game->StartGame(&attribs, ""); // TODO: Non progressive load can fail - need a decent way to handle this LDR_NonprogressiveLoad(); PSRETURN ret = g_Game->ReallyStartGame(); ENSURE(ret == PSRETURN_OK); } else if (type == "turn") { *m_Stream >> turn >> turnLength; debug_printf("Turn %u (%u)...\n", turn, turnLength); } else if (type == "cmd") { player_id_t player; *m_Stream >> player; std::string line; std::getline(*m_Stream, line); ScriptRequest rq(g_Game->GetSimulation2()->GetScriptInterface()); JS::RootedValue data(rq.cx); Script::ParseJSON(rq, line, &data); Script::FreezeObject(rq, data, true); commands.emplace_back(SimulationCommand(player, rq.cx, data)); } else if (type == "hash" || type == "hash-quick") { std::string replayHash; *m_Stream >> replayHash; TestHash(type, replayHash, testHashFull, testHashQuick); } else if (type == "end") { { g_Profiler2.RecordFrameStart(); PROFILE2("frame"); g_Profiler2.IncrementFrameNumber(); PROFILE2_ATTR("%d", g_Profiler2.GetFrameNumber()); g_Game->GetSimulation2()->Update(turnLength, commands); commands.clear(); } g_Profiler.Frame(); if (turn % PROFILE_TURN_INTERVAL == 0) g_ProfileViewer.SaveToFile(); } else debug_printf("Unrecognised replay token %s\n", type.c_str()); } } SAFE_DELETE(m_Stream); g_Profiler2.SaveToFile(); std::string hash; bool ok = g_Game->GetSimulation2()->ComputeStateHash(hash, false); ENSURE(ok); debug_printf("# Final state: %s\n", Hexify(hash).c_str()); timer_DisplayClientTotals(); SAFE_DELETE(g_Game); // Must be explicitly destructed here to avoid callbacks from the JSAPI trying to use g_Profiler2 when // it's already destructed. g_ScriptContext.reset(); delete &g_Profiler; delete &g_ProfileViewer; SAFE_DELETE(g_ScriptStatsTable); } void CReplayPlayer::TestHash(const std::string& hashType, const std::string& replayHash, const bool testHashFull, const bool testHashQuick) { bool quick = (hashType == "hash-quick"); if ((quick && !testHashQuick) || (!quick && !testHashFull)) return; std::string hash; ENSURE(g_Game->GetSimulation2()->ComputeStateHash(hash, quick)); std::string hexHash = Hexify(hash); if (hexHash == replayHash) debug_printf("%s ok (%s)\n", hashType.c_str(), hexHash.c_str()); else debug_printf("%s MISMATCH (%s != %s)\n", hashType.c_str(), hexHash.c_str(), replayHash.c_str()); }