00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <common/OptionsScorched.h>
00022 #include <common/Logger.h>
00023 #include <engine/ScorchedContext.h>
00024 #include <landscapedef/LandscapeTex.h>
00025 #include <landscapedef/LandscapeDefn.h>
00026 #include <landscapedef/LandscapeDefinitions.h>
00027 #include <landscapedef/LandscapeOptions.h>
00028 #include <landscapedef/LandscapeInclude.h>
00029 #include <net/NetBufferPool.h>
00030
00031 OptionsScorched::OptionsScorched()
00032 {
00033 }
00034
00035 OptionsScorched::~OptionsScorched()
00036 {
00037 }
00038
00039 void OptionsScorched::updateLevelOptions(ScorchedContext &context, LandscapeDefinition &defn)
00040 {
00041
00042 LandscapeTex *ltex = context.getLandscapes().getTex(defn.getTex());
00043 LandscapeDefn *ldefn = context.getLandscapes().getDefn(defn.getDefn());
00044
00045
00046 std::map<std::string, OptionEntry *> values;
00047 updateLevelOptions(ltex->texDefn.includes, values);
00048 updateLevelOptions(ldefn->texDefn.includes, values);
00049
00050
00051 std::list<OptionEntry *> &levelOptions = getLevelOptions().getOptions();
00052 std::list<OptionEntry *> &mainoptions = getMainOptions().getOptions();
00053 std::list<OptionEntry *>::iterator levelitor;
00054 std::list<OptionEntry *>::iterator mainitor;
00055 for (levelitor = levelOptions.begin(), mainitor = mainoptions.begin();
00056 levelitor != levelOptions.end() && mainitor != mainoptions.end();
00057 levelitor++, mainitor++)
00058 {
00059 OptionEntry *mainEntry = (*mainitor);
00060 OptionEntry *levelEntry = (*levelitor);
00061
00062
00063 OptionEntry *currentEntry = mainEntry;
00064 if (levelEntry->isChangedValue()) currentEntry = levelEntry;
00065 std::string oldValue = currentEntry->getValueAsString();
00066
00067
00068 levelEntry->setNotChanged();
00069
00070
00071 std::map<std::string, OptionEntry *>::iterator findItor =
00072 values.find(mainEntry->getName());
00073 if (findItor != values.end())
00074 {
00075 levelEntry->setValueFromString((*findItor).second->getValueAsString());
00076 }
00077
00078
00079 currentEntry = mainEntry;
00080 if (levelEntry->isChangedValue()) currentEntry = levelEntry;
00081 std::string newValue = currentEntry->getValueAsString();
00082
00083
00084 if (0 != strcmp(newValue.c_str(), oldValue.c_str()))
00085 {
00086 Logger::log(S3D::formatStringBuffer("Level option %s has been changed from %s to %s",
00087 mainEntry->getName(),
00088 oldValue.c_str(), newValue.c_str()));
00089 }
00090 }
00091 }
00092
00093 void OptionsScorched::updateLevelOptions(std::vector<LandscapeInclude *> &options,
00094 std::map<std::string, OptionEntry *> &values)
00095 {
00096
00097 std::vector<LandscapeInclude *>::iterator itor;
00098 for (itor = options.begin();
00099 itor != options.end();
00100 itor++)
00101 {
00102 LandscapeInclude *option = (*itor);
00103
00104
00105 std::vector<LandscapeOptionsType *>::iterator typeItor;
00106 for (typeItor = option->options.begin();
00107 typeItor != option->options.end();
00108 typeItor ++)
00109 {
00110 LandscapeOptionsType *optionType = (*typeItor);
00111
00112
00113 std::list<OptionEntry *>::iterator srcitor;
00114 for (srcitor = optionType->options.getOptions().begin();
00115 srcitor != optionType->options.getOptions().end();
00116 srcitor++)
00117 {
00118 OptionEntry *srcEntry = (*srcitor);
00119 if (srcEntry->isChangedValue())
00120 {
00121 values[srcEntry->getName()] = srcEntry;
00122 }
00123 }
00124 }
00125 }
00126 }
00127
00128 void OptionsScorched::updateChangeSet()
00129 {
00130 NetBuffer *defaultBuffer = NetBufferPool::instance()->getFromPool();
00131
00132 defaultBuffer->reset();
00133 mainOptions_.writeToBuffer(*defaultBuffer, true, true);
00134 NetBufferReader reader(*defaultBuffer);
00135 changedOptions_.readFromBuffer(reader, true, true);
00136
00137 NetBufferPool::instance()->addToPool(defaultBuffer);
00138 }
00139
00140 bool OptionsScorched::commitChanges()
00141 {
00142 bool different = false;
00143
00144
00145 std::list<OptionEntry *> &options = mainOptions_.getOptions();
00146 std::list<OptionEntry *> &otheroptions = changedOptions_.getOptions();
00147 std::list<OptionEntry *>::iterator itor;
00148 std::list<OptionEntry *>::iterator otheritor;
00149 for (itor=options.begin(), otheritor=otheroptions.begin();
00150 itor!=options.end() && otheritor!=otheroptions.end();
00151 itor++, otheritor++)
00152 {
00153 OptionEntry *entry = *itor;
00154 OptionEntry *otherentry = *otheritor;
00155
00156 DIALOG_ASSERT(0 == strcmp(entry->getName(), otherentry->getName()));
00157
00158 std::string str = entry->getValueAsString();
00159 std::string otherstr = otherentry->getValueAsString();
00160 if (str != otherstr)
00161 {
00162 if (!(entry->getData() & OptionEntry::DataProtected) &&
00163 !(otherentry->getData() & OptionEntry::DataProtected))
00164 {
00165 if (strlen(str.c_str()) < 20 && strlen(otherstr.c_str()) < 20)
00166 {
00167 Logger::log(S3D::formatStringBuffer("Option %s has been changed from %s to %s",
00168 entry->getName(), str.c_str(), otherstr.c_str()));
00169 }
00170 else
00171 {
00172 Logger::log(S3D::formatStringBuffer("Option %s has been changed.",
00173 entry->getName()));
00174 }
00175 }
00176
00177 different = true;
00178 entry->setValueFromString(otherentry->getValueAsString());
00179 }
00180 }
00181
00182 return different;
00183 }