From cf4aea531d32e377da24bcb7d547624d2ca726d7 Mon Sep 17 00:00:00 2001 From: coppro Date: Wed, 7 Jan 2009 00:20:10 +0000 Subject: [PATCH] Game settings loading is now deferred until the game is fully loading. This allows auto replaces to be properly loaded from the game file. git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1295 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/data/settings.cpp | 6 ++- src/data/word_list.hpp | 12 +++--- src/gui/auto_replace_window.cpp | 22 +++++------ src/util/io/package.hpp | 66 +++++++++++++++++---------------- 4 files changed, 57 insertions(+), 49 deletions(-) diff --git a/src/data/settings.cpp b/src/data/settings.cpp index 0e52a058..2f819c8a 100644 --- a/src/data/settings.cpp +++ b/src/data/settings.cpp @@ -81,7 +81,11 @@ GameSettings::GameSettings() {} void GameSettings::initDefaults(const Game& game) { - if (initialized) return; + // Defer initialization until the game is fully loaded. + // This prevents data that needs to be initialized from + // being accessed from the new set window, but removes + // the need to load the entire file, which takes too long. + if (initialized || !game.isFullyLoaded()) return; initialized = true; // init auto_replaces, copy from game file FOR_EACH_CONST(ar, game.auto_replaces) { diff --git a/src/data/word_list.hpp b/src/data/word_list.hpp index dabe14f6..79e4d5dd 100644 --- a/src/data/word_list.hpp +++ b/src/data/word_list.hpp @@ -23,15 +23,15 @@ DECLARE_POINTER_TYPE(AutoReplace); class WordListWord : public IntrusivePtrBase { public: WordListWord(); - + String name; ///< Name of the list / the word bool line_below; ///< Line below in the list? bool is_prefix; ///< Is this a prefix before other words? vector words; ///< Sublist OptionalScript script; ///< Generate words using a script - + inline bool isGroup() const { return !words.empty(); } - + DECLARE_REFLECTION(); }; @@ -47,15 +47,15 @@ class WordList : public WordListWord { class AutoReplace : public IntrusivePtrVirtualBase { public: AutoReplace(); - + bool enabled; bool whole_word; bool custom; ///< Is this a custom auto replace? String match; String replace; - + inline AutoReplaceP clone() const { return new_intrusive1(*this); } - + DECLARE_REFLECTION(); }; diff --git a/src/gui/auto_replace_window.cpp b/src/gui/auto_replace_window.cpp index 9b8af401..3524b204 100644 --- a/src/gui/auto_replace_window.cpp +++ b/src/gui/auto_replace_window.cpp @@ -27,38 +27,38 @@ DEFINE_EVENT_TYPE(EVENT_ITEM_SELECT); class AutoReplaceList : public ItemList { public: AutoReplaceList(Window* parent, int id, const Game& game); - + /// The items vector items; /// Settings we are edditing const Game& game; GameSettings& gs; - + /// The current item inline AutoReplaceP getSelected() const { return static_pointer_cast(selected_item); } - + /// Add an item void addItem(const AutoReplaceP& item); /// Remove the selected item void removeSelected(); - + /// Reset the list to the default void reset(); - + using ItemList::refreshList; - + protected: /// Get all items virtual void getItems(vector& out) const; /// Return the AutoReplace at the given position in the sorted list inline AutoReplaceP getAR(long pos) const { return static_pointer_cast(getItem(pos)); } - + /// Send an 'item selected' event for the currently selected item (selected_item) virtual void sendEvent(); /// Compare items virtual bool compareItems(void* a, void* b) const; virtual bool mustSort() const { return true; } - + /// Get the text of an item in a specific column /** Overrides a function from wxListCtrl */ virtual String OnGetItemText (long pos, long col) const; @@ -67,7 +67,7 @@ class AutoReplaceList : public ItemList { virtual int OnGetItemImage(long pos) const; /// Get the color for an item virtual wxListItemAttr* OnGetItemAttr(long pos) const; - + mutable wxListItemAttr item_attr; // for OnGetItemAttr }; @@ -81,7 +81,7 @@ AutoReplaceList::AutoReplaceList(Window* parent, int id, const Game& game) items.push_back(ar->clone()); } // Add columns - InsertColumn(0, _LABEL_(""), wxLIST_FORMAT_LEFT, 0); // dummy, prevent the image from taking up space + InsertColumn(0, _(""), wxLIST_FORMAT_LEFT, 0); // dummy, prevent the image from taking up space InsertColumn(1, _LABEL_("auto match"), wxLIST_FORMAT_LEFT, 100); InsertColumn(2, _LABEL_("auto replace"), wxLIST_FORMAT_LEFT, 200); // grey for disabled items @@ -143,7 +143,7 @@ String AutoReplaceList::OnGetItemText (long pos, long col) const { if (col == 0) return ar->match; if (col == 1) return ar->match; if (col == 2) return ar->replace; - throw InternalError(_("too mana columns")); + throw InternalError(_("too many columns")); } int AutoReplaceList::OnGetItemImage(long pos) const { diff --git a/src/util/io/package.hpp b/src/util/io/package.hpp index 4513542a..ecd5122d 100644 --- a/src/util/io/package.hpp +++ b/src/util/io/package.hpp @@ -38,7 +38,7 @@ DECLARE_DYNAMIC_ARG(Package*, clipboard_package); * * Zip files are accessed using wxZip(Input|Output)Stream. * The zip input stream appears to only allow one file at a time, since the stream itself maintains - * state about what file we are reading. + * state about what file we are reading. * There are multiple solutions: * 1. (currently used) Open a new ZipInputStream for each file * 2. (may be faster) First read the file into a memory buffer, @@ -49,11 +49,11 @@ DECLARE_DYNAMIC_ARG(Package*, clipboard_package); class Package : public IntrusivePtrVirtualBase { public: // --------------------------------------------------- : Managing the outside of the package - + /// Creates a new package Package(); virtual ~Package(); - + /// Is a file opened? bool isOpened() const; /// Must the package be saved with saveAs()? @@ -67,11 +67,11 @@ class Package : public IntrusivePtrVirtualBase { const String& absoluteFilename() const; /// The time this package was last modified inline wxDateTime lastModified() const { return modified; } - + /// Open a package, should only be called when the package is constructed using the default constructor! /// @pre open not called before [TODO] void open(const String& package); - + /// Saves the package, by default saves as a zip file, unless /// it was already a directory /** If remove_unused=true all files that were in the file and @@ -79,46 +79,46 @@ class Package : public IntrusivePtrVirtualBase { * This is a form of garbage collection, to get rid of old picture files for example. */ void save(bool remove_unused = true); - + /// Saves the package under a different filename void saveAs(const String& package, bool remove_unused = true); - + /// Saves the package under a different filename, but keep the old one open void saveCopy(const String& package); - - + + // --------------------------------------------------- : Managing the inside of the package - + /// Open an input stream for a file in the package. InputStreamP openIn(const String& file); - + /// Open an output stream for a file in the package. /// (changes are only committed with save()) OutputStreamP openOut(const String& file); - + /// Get a filename that can be written to to modfify a file in the package /// (changes are only committed with save()) String nameOut(const String& file); - + /// Creates a new, unique, filename with the specified prefix and suffix /// for example newFileName("image/",".jpg") -> "image/1.jpg" /// Returns the name of a temporary file that can be written to. FileName newFileName(const String& prefix, const String& suffix); - + /// Signal that a file is still used by this package. /// Must be called for files not opened using openOut/nameOut /// If they are to be kept in the package. void referenceFile(const String& file); - + /// Get an 'absolute filename' for a file in the package. /// This file can later be opened from anywhere (other process) using openAbsoluteFile() String absoluteName(const String& file); - + /// Open a file given an absolute filename static InputStreamP openAbsoluteFile(const String& name); - + // --------------------------------------------------- : Managing the inside of the package : Reader/writer - + template void readFile(const String& file, T& obj) { Reader reader(openIn(file), dynamic_cast(this), absoluteFilename() + _("/") + file); @@ -134,16 +134,16 @@ class Package : public IntrusivePtrVirtualBase { readFile(file, obj); return obj; } - + template void writeFile(const String& file, const T& obj, Version file_version) { Writer writer(openOut(file), file_version); writer.handle(obj); } - + // --------------------------------------------------- : Private stuff private: - + /// Information about a file in the package struct FileInfo { FileInfo(); @@ -154,7 +154,7 @@ class Package : public IntrusivePtrVirtualBase { /// Is this file changed, and therefore written to a temporary file? inline bool wasWritten() const { return !tempName.empty(); } }; - + /// Filename of the package String filename; /// Last modified time @@ -173,7 +173,7 @@ class Package : public IntrusivePtrVirtualBase { wxFileInputStream* fileStream; /// Filestream for reading zip files wxZipInputStream* zipStream; - + void loadZipStream(); void openDirectory(); void openSubdir(const String&); @@ -193,7 +193,7 @@ class PackageDependency : public IntrusivePtrBase { public: String package; ///< Name of the package someone depends on Version version; ///< Minimal required version of that package - + DECLARE_REFLECTION(); }; @@ -204,7 +204,7 @@ class Packaged : public Package { public: Packaged(); virtual ~Packaged() {} - + Version version; ///< Version number of this package Version compatible_version; ///< Earliest version number this package is compatible with String installer_group; ///< Group to place this package in in the installer @@ -213,10 +213,10 @@ class Packaged : public Package { String icon_filename; ///< Filename of icon to use in package lists vector dependencies; ///< Dependencies of this package int position_hint; ///< A hint for the package list - + /// Get an input stream for the package icon, if there is any InputStreamP openIconFile(); - + /// Open a package, and read the data /** if just_header is true, then the package is not fully parsed. */ @@ -226,11 +226,15 @@ class Packaged : public Package { void save(); void saveAs(const String& package, bool remove_unused = true); void saveCopy(const String& package); - + /// Check if this package lists a dependency on the given package /** This is done to force people to fill in the dependencies */ void requireDependency(Packaged* package); - + + inline bool isFullyLoaded () const { + return fully_loaded; + } + protected: /// filename of the data file, and extension of the package file virtual String typeName() const = 0; @@ -238,9 +242,9 @@ class Packaged : public Package { virtual void validate(Version file_app_version); /// What file version should be used for writing files? virtual Version fileVersion() const = 0; - + DECLARE_REFLECTION_VIRTUAL(); - + private: bool fully_loaded; ///< Is the package fully loaded? friend struct JustAsPackageProxy;