Merge persistence branch back into trunk as per Crab's instruction.

This commit is contained in:
Jody Northup 2010-05-02 22:55:34 +00:00
commit 82b2224ca2
12 changed files with 2745 additions and 2370 deletions

View file

@ -76,6 +76,9 @@ Version 1.9.0-svn:
* Extended #ifdef so that it also tests for files and directories
* Add [music_volume] tag, which permits modifications to music volume during scenario
* Added ON_DIFFICULTY, a macro that makes using different values based on difficulty simpler
* Prototype support for [set_global_variable]
* Prototype support for [get_global_variable]
* Prototype support for [clear_global_variable]
* Miscellaneous and bug fixes:
* Defaulted log level to warning again
* New option to auto-set delay shroud update on game start

View file

@ -912,6 +912,10 @@
[entry]
name = "Jim Carroll (Jimm)"
[/entry]
[entry]
name = "Jody Northup (Upthorn)"
comment = "Prospective student (world persistence)"
[/entry]
[entry]
name = "Joeri Melis"
[/entry]

File diff suppressed because it is too large Load diff

View file

@ -1,344 +1,336 @@
<?xml version="1.0" encoding="windows-1250"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9,00"
Name="wesnothlib"
ProjectGUID="{4A16638B-127B-4AE8-8E0B-4B211B96F976}"
RootNamespace="wesnothlib"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)\wesnothlib"
ConfigurationType="4"
CharacterSet="2"
BuildLogFile="$(IntDir)\BuildLog.htm"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="../../src"
PreprocessorDefinitions="WIN32;_WINDOWS;_CRT_SECURE_NO_WARNINGS;HAVE_PYTHON;USE_GZIP;NO_HAVE_FRIBIDI;NOMINMAX"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLibrarianTool"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="4"
CharacterSet="2"
WholeProgramOptimization="1"
BuildLogFile="$(IntDir)\BuildLog-wesnothlib.htm"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
AdditionalIncludeDirectories="../../src"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;HAVE_PYTHON;USE_GZIP;NO_HAVE_FRIBIDI;NOMINMAX"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLibrarianTool"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\..\src\color_range.cpp"
>
</File>
<File
RelativePath="..\..\src\config.cpp"
>
</File>
<File
RelativePath="..\..\src\filesystem.cpp"
>
</File>
<File
RelativePath="..\..\src\game_config.cpp"
>
</File>
<File
RelativePath="..\..\src\gettext.cpp"
>
</File>
<File
RelativePath="..\..\src\log.cpp"
>
</File>
<File
RelativePath="..\..\src\network.cpp"
>
</File>
<File
RelativePath="..\..\src\network_worker.cpp"
>
</File>
<File
RelativePath="..\..\src\thread.cpp"
>
</File>
<File
RelativePath="..\..\src\time.cpp"
>
</File>
<File
RelativePath="..\..\src\tstring.cpp"
>
</File>
<File
RelativePath="..\..\src\util.cpp"
>
</File>
<File
RelativePath="..\..\src\version.cpp"
>
</File>
<Filter
Name="Serialization"
>
<File
RelativePath="..\..\src\serialization\binary_or_text.cpp"
>
</File>
<File
RelativePath="..\..\src\serialization\binary_wml.cpp"
>
</File>
<File
RelativePath="..\..\src\serialization\parser.cpp"
>
</File>
<File
RelativePath="..\..\src\serialization\preprocessor.cpp"
>
</File>
<File
RelativePath="..\..\src\serialization\string_utils.cpp"
>
</File>
<File
RelativePath="..\..\src\serialization\tokenizer.cpp"
>
</File>
</Filter>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath="..\..\src\about.hpp"
>
</File>
<File
RelativePath="..\..\src\config.hpp"
>
</File>
<File
RelativePath="..\..\src\filesystem.hpp"
>
</File>
<File
RelativePath="..\..\src\game_config.hpp"
>
</File>
<File
RelativePath="..\..\src\gettext.hpp"
>
</File>
<File
RelativePath="..\..\src\global.hpp"
>
</File>
<File
RelativePath="..\..\src\log.hpp"
>
</File>
<File
RelativePath="..\..\src\map.hpp"
>
</File>
<File
RelativePath="..\..\src\network.hpp"
>
</File>
<File
RelativePath="..\..\src\network_worker.hpp"
>
</File>
<File
RelativePath="..\..\src\scoped_resource.hpp"
>
</File>
<File
RelativePath="..\..\src\thread.hpp"
>
</File>
<File
RelativePath="..\..\src\time.hpp"
>
</File>
<File
RelativePath="..\..\src\tstring.hpp"
>
</File>
<File
RelativePath="..\..\src\util.hpp"
>
</File>
<File
RelativePath="..\..\src\version.hpp"
>
</File>
<File
RelativePath="..\..\src\wesconfig.h"
>
</File>
<Filter
Name="Serialization"
>
<File
RelativePath="..\..\src\serialization\binary_or_text.hpp"
>
</File>
<File
RelativePath="..\..\src\serialization\binary_wml.hpp"
>
</File>
<File
RelativePath="..\..\src\serialization\parser.hpp"
>
</File>
<File
RelativePath="..\..\src\serialization\preprocessor.hpp"
>
</File>
<File
RelativePath="..\..\src\serialization\string_utils.hpp"
>
</File>
<File
RelativePath="..\..\src\serialization\tokenizer.hpp"
>
</File>
</Filter>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>
<?xml version="1.0" encoding="windows-1250"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="wesnothlib"
ProjectGUID="{4A16638B-127B-4AE8-8E0B-4B211B96F976}"
RootNamespace="wesnothlib"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)\wesnothlib"
ConfigurationType="4"
CharacterSet="2"
BuildLogFile="$(IntDir)\BuildLog.htm"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="../../src"
PreprocessorDefinitions="WIN32;_WINDOWS;_CRT_SECURE_NO_WARNINGS;HAVE_PYTHON;USE_GZIP;NO_HAVE_FRIBIDI;NOMINMAX;_DEBUG"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLibrarianTool"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="4"
CharacterSet="2"
WholeProgramOptimization="1"
BuildLogFile="$(IntDir)\BuildLog-wesnothlib.htm"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
AdditionalIncludeDirectories="../../src"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;HAVE_PYTHON;USE_GZIP;NO_HAVE_FRIBIDI;NOMINMAX"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLibrarianTool"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\..\src\color_range.cpp"
>
</File>
<File
RelativePath="..\..\src\config.cpp"
>
</File>
<File
RelativePath="..\..\src\filesystem.cpp"
>
</File>
<File
RelativePath="..\..\src\game_config.cpp"
>
</File>
<File
RelativePath="..\..\src\gettext.cpp"
>
</File>
<File
RelativePath="..\..\src\log.cpp"
>
</File>
<File
RelativePath="..\..\src\network.cpp"
>
</File>
<File
RelativePath="..\..\src\network_worker.cpp"
>
</File>
<File
RelativePath="..\..\src\thread.cpp"
>
</File>
<File
RelativePath="..\..\src\tstring.cpp"
>
</File>
<File
RelativePath="..\..\src\util.cpp"
>
</File>
<File
RelativePath="..\..\src\version.cpp"
>
</File>
<Filter
Name="Serialization"
>
<File
RelativePath="..\..\src\serialization\binary_or_text.cpp"
>
</File>
<File
RelativePath="..\..\src\serialization\binary_wml.cpp"
>
</File>
<File
RelativePath="..\..\src\serialization\parser.cpp"
>
</File>
<File
RelativePath="..\..\src\serialization\preprocessor.cpp"
>
</File>
<File
RelativePath="..\..\src\serialization\string_utils.cpp"
>
</File>
<File
RelativePath="..\..\src\serialization\tokenizer.cpp"
>
</File>
</Filter>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath="..\..\src\about.hpp"
>
</File>
<File
RelativePath="..\..\src\config.hpp"
>
</File>
<File
RelativePath="..\..\src\filesystem.hpp"
>
</File>
<File
RelativePath="..\..\src\game_config.hpp"
>
</File>
<File
RelativePath="..\..\src\gettext.hpp"
>
</File>
<File
RelativePath="..\..\src\global.hpp"
>
</File>
<File
RelativePath="..\..\src\log.hpp"
>
</File>
<File
RelativePath="..\..\src\map.hpp"
>
</File>
<File
RelativePath="..\..\src\network.hpp"
>
</File>
<File
RelativePath="..\..\src\network_worker.hpp"
>
</File>
<File
RelativePath="..\..\src\scoped_resource.hpp"
>
</File>
<File
RelativePath="..\..\src\thread.hpp"
>
</File>
<File
RelativePath="..\..\src\tstring.hpp"
>
</File>
<File
RelativePath="..\..\src\util.hpp"
>
</File>
<File
RelativePath="..\..\src\version.hpp"
>
</File>
<File
RelativePath="..\..\src\wesconfig.h"
>
</File>
<Filter
Name="Serialization"
>
<File
RelativePath="..\..\src\serialization\binary_or_text.hpp"
>
</File>
<File
RelativePath="..\..\src\serialization\binary_wml.hpp"
>
</File>
<File
RelativePath="..\..\src\serialization\parser.hpp"
>
</File>
<File
RelativePath="..\..\src\serialization\preprocessor.hpp"
>
</File>
<File
RelativePath="..\..\src\serialization\string_utils.hpp"
>
</File>
<File
RelativePath="..\..\src\serialization\tokenizer.hpp"
>
</File>
</Filter>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View file

@ -400,6 +400,8 @@ set(wesnoth-main_SRC
multiplayer_create.cpp
network.cpp
network_worker.cpp
persist_context.cpp
persist_var.cpp
playcampaign.cpp
play_controller.cpp
playmp_controller.cpp

View file

@ -229,12 +229,13 @@ wesnoth_source = \
network.cpp \
network_worker.cpp \
pathfind/pathfind.cpp \
persist_context.cpp \
persist_var.cpp \
playcampaign.cpp \
play_controller.cpp \
playmp_controller.cpp \
playsingle_controller.cpp \
playturn.cpp \
portrait.cpp \
replay.cpp \
replay_controller.cpp \
resources.cpp \

View file

@ -216,6 +216,8 @@ wesnoth_sources = Split("""
multiplayer_create.cpp
multiplayer_connect.cpp
pathfind/pathfind.cpp
persist_context.cpp
persist_var.cpp
playcampaign.cpp
play_controller.cpp
playmp_controller.cpp

View file

@ -44,6 +44,7 @@
#include "unit_display.hpp"
#include "wml_exception.hpp"
#include "play_controller.hpp"
#include "persist_var.hpp"
#include <boost/scoped_ptr.hpp>
#include <boost/lexical_cast.hpp>
@ -3050,6 +3051,20 @@ WML_HANDLER_FUNCTION(replace_map, /*event_info*/, cfg)
ai::manager::raise_map_changed();
}
// Experimental data persistence
WML_HANDLER_FUNCTION(set_global_variable,/**/,pcfg)
{
verify_and_set_global_variable(pcfg);
}
WML_HANDLER_FUNCTION(get_global_variable,/**/,pcfg)
{
verify_and_get_global_variable(pcfg);
}
WML_HANDLER_FUNCTION(clear_global_variable,/**/,pcfg)
{
verify_and_clear_global_variable(pcfg);
}
/** Handles all the different types of actions that can be triggered by an event. */
static void commit_new_handlers() {

163
src/persist_context.cpp Normal file
View file

@ -0,0 +1,163 @@
/* $Id: persist_var.cpp 42098 2010-04-10 17:30:45Z upthorn $ */
/*
Copyright (C) 2003 - 2010 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "global.hpp"
#include "filesystem.hpp"
#include "util.hpp"
#include "log.hpp"
#include "persist_context.hpp"
#include "resources.hpp"
#include "serialization/binary_or_text.hpp"
bool is_valid_namespace(const std::string &name_space)
{
return (name_space.find_first_not_of("!'(),-0123456789;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_") > name_space.length());
}
std::string get_persist_cfg_name(const std::string &name_space)
{
// TODO: get namespace base name
if (is_valid_namespace(name_space))
return (get_dir(get_user_data_dir() + "/persist/") + name_space + ".cfg");
else
return "";
}
bool load_persist_data(const std::string &name_space, config &cfg, const bool create_if_missing = true)
{
bool success = false;
std::string cfg_dir = get_dir(get_user_data_dir() + "/persist");
create_directory_if_missing(cfg_dir);
// TODO: Support nested namespaces
std::string cfg_name = get_persist_cfg_name(name_space);
if (!cfg_name.empty()) {
scoped_istream file_stream = istream_file(cfg_name);
if (file_stream->fail()) {
if (create_if_missing) {
// TODO: create proper file structure.
success = true;
}
} else {
try {
detect_format_and_read(cfg,*file_stream);
success = true;
} catch (config::error &err) {
LOG_SAVE << err.message;
success = false;
}
}
}
return success;
}
bool save_persist_data(std::string &name_space, config &cfg)
{
bool success = false;
// TODO: Support nested namespaces
std::string cfg_name = get_persist_cfg_name(name_space);
if (!cfg_name.empty()) {
if (cfg.empty()) {
success = delete_directory(cfg_name);
} else {
scoped_ostream out = ostream_file(cfg_name);
if (!out->fail())
{
config_writer writer(*out,false);
try {
writer.write(cfg);
success = true;
} catch(config::error &err) {
LOG_SAVE << err.message;
success = false;
}
}
}
}
return success;
}
config pack_scalar(const std::string &name, const t_string &val)
{
config cfg;
cfg[name] = val;
return cfg;
}
// TODO: support nested namespaces
persist_context::persist_context(const std::string &name_space) : namespace_(name_space), cfg_(), valid_(is_valid_namespace(namespace_.substr(0,name_space.find_first_of(".")))) {}
bool persist_context::clear_var(std::string &global)
{
if (cfg_.empty()) {
load_persist_data(namespace_,cfg_);
}
// TODO: get config's variables.
bool exists = cfg_.has_attribute(global);
bool ret;
if (!exists) {
if (cfg_.child(global)) {
exists = true;
std::string::iterator index_start = std::find(global.begin(),global.end(),'[');
if (index_start != global.end())
{
const std::string::iterator index_end = std::find(global.begin(),global.end(),']');
const std::string index_str(index_start+1,index_end);
size_t index = static_cast<size_t>(lexical_cast_default<int>(index_str));
cfg_.remove_child(global,index);
} else {
cfg_.clear_children(global);
}
}
}
if (exists) {
cfg_.remove_attribute(global);
ret = save_persist_data(namespace_,cfg_);
} else {
ret = exists;
}
return ret;
}
config persist_context::get_var(const std::string &global)
{
config ret;
if (cfg_.empty()) {
load_persist_data(namespace_,cfg_,false);
}
//TODO: get config's [variables]
if (cfg_.child(global)) {
ret.add_child(global,cfg_.child(global));
} else {
ret = pack_scalar(global,cfg_[global]);
}
return ret;
}
bool persist_context::set_var(const std::string &global,const config &val)
{
if (cfg_.empty()) {
load_persist_data(namespace_,cfg_);
}
//TODO: get config's [variables]
if (val.has_attribute(global)) {
cfg_[global] = val[global];
} else {
cfg_.add_child(global,val.child(global));
}
return save_persist_data(namespace_,cfg_);
}

38
src/persist_context.hpp Normal file
View file

@ -0,0 +1,38 @@
/* $Id$ */
/*
Copyright (C) 2003 - 2010 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef PERSIST_CONTEXT_H_INCLUDED
#define PERSIST_CONTEXT_H_INCLUDED
#include "config.hpp"
#include "log.hpp"
static lg::log_domain log_persist("engine/persistence");
#define LOG_SAVE LOG_STREAM(info, log_persist)
#define ERR_SAVE LOG_STREAM(err, log_persist)
config pack_scalar(const std::string &,const t_string &);
class persist_context {
private:
// TODO: dirty marking
// TODO: child persist_contexts for embedded namespaces?
config cfg_;
std::string namespace_;
bool valid_;
public:
persist_context(const std::string &);
bool clear_var(std::string &);
config get_var(const std::string &);
bool set_var(const std::string &, const config &);
bool valid() const { return valid_; };
};
#endif

134
src/persist_var.cpp Normal file
View file

@ -0,0 +1,134 @@
/* $Id: persist_var.cpp 42098 2010-04-10 17:30:45Z upthorn $ */
/*
Copyright (C) 2003 - 2010 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "global.hpp"
#include "gamestatus.hpp"
#include "log.hpp"
#include "persist_context.hpp"
#include "persist_var.hpp"
#include "resources.hpp"
void get_global_variable(persist_context &ctx, const vconfig &pcfg)
{
std::string global = pcfg["from_global"];
std::string local = pcfg["to_local"];
config cfg = ctx.get_var(global);
if (cfg) {
if (cfg.has_attribute(global)) {
resources::state_of_game->set_variable(local,cfg[global]);
} else {
resources::state_of_game->clear_variable(local);
resources::state_of_game->add_variable_cfg(local,cfg.child(global));
}
} else {
resources::state_of_game->set_variable(local,"");
}
}
void clear_global_variable(persist_context &ctx, const vconfig &pcfg)
{
std::string global = pcfg["global"];
ctx.clear_var(global);
}
void set_global_variable(persist_context &ctx, const vconfig &pcfg)
{
if (pcfg["from_local"].empty()) {
clear_global_variable(ctx, pcfg);
} else {
std::string global = pcfg["to_global"];
std::string local = pcfg["from_local"];
config val;
val.add_child(global,resources::state_of_game->get_variable_cfg(local));
if (val.child(global).empty()) {
val = pack_scalar(global,resources::state_of_game->get_variable(local));
}
ctx.set_var(global,val);
}
}
void verify_and_get_global_variable(const vconfig &pcfg)
{
bool valid = true;
if (!pcfg.has_attribute("from_global")) {
LOG_SAVE << "Error: [get_global_variable] missing required attribute \"from_global\"";
valid = false;
}
if (!pcfg.has_attribute("to_local")) {
LOG_SAVE << "Error: [get_global_variable] missing required attribute \"to_local\"";
valid = false;
}
// TODO: allow for global namespace.
if (!pcfg.has_attribute("namespace")) {
LOG_SAVE << "Error: [get_global_variable] missing attribute \"namespace\" and no global namespace provided.";
valid = false;
}
// TODO: determine single or multiplayer and check for side=, depending.
if (valid)
{
persist_context ctx(pcfg["namespace"]);
if (ctx.valid()) {
get_global_variable(ctx,pcfg);
} else {
LOG_SAVE << "Error: [get_global_variable] attribute \"namespace\" is not valid.";
}
}
}
void verify_and_set_global_variable(const vconfig &pcfg)
{
bool valid = true;
if (!pcfg.has_attribute("to_global")) {
LOG_SAVE << "Error: [set_global_variable] missing required attribute \"from_global\"";
valid = false;
}
if (!pcfg.has_attribute("from_local")) {
LOG_SAVE << "Warning: [set_global_variable] missing attribute \"to_local\", global variable will be cleared";
}
// TODO: allow for global namespace.
if (!pcfg.has_attribute("namespace")) {
LOG_SAVE << "Error: [set_global_variable] missing attribute \"namespace\" and no global namespace provided.";
valid = false;
}
// TODO: determine single or multiplayer and check for side=, depending.
if (valid)
{
persist_context ctx(pcfg["namespace"]);
if (ctx.valid()) {
set_global_variable(ctx,pcfg);
} else {
LOG_SAVE << "Error: [set_global_variable] attribute \"namespace\" is not valid.";
}
}
}
void verify_and_clear_global_variable(const vconfig &pcfg)
{
bool valid = true;
if (!pcfg.has_attribute("global")) {
LOG_SAVE << "Error: [clear_global_variable] missing required attribute \"from_global\"";
valid = false;
}
if (!pcfg.has_attribute("namespace")) {
LOG_SAVE << "Error: [clear_global_variable] missing attribute \"namespace\" and no global namespace provided.";
valid = false;
}
// TODO: determine single or multiplayer and check for side=, depending.
if (valid)
{
persist_context ctx(pcfg["namespace"]);
if (ctx.valid()) {
clear_global_variable(ctx,pcfg);
} else {
LOG_SAVE << "Error: [clear_global_variable] attribute \"namespace\" is not valid.";
}
}
}

20
src/persist_var.hpp Normal file
View file

@ -0,0 +1,20 @@
/* $Id: persist_var.hpp 41840 2010-03-28 10:42:41Z upthorn $ */
/*
Copyright (C) 2003 - 2010 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef PERSIST_VAR_H_INCLUDED
#define PERSIST_VAR_H_INCLUDED
void verify_and_set_global_variable(const vconfig &pcfg);
void verify_and_get_global_variable(const vconfig &pcfg);
void verify_and_clear_global_variable(const vconfig &pcfg);
#endif