/// <summary>Opens a document template.</summary>
   /// <param name="docPath">document path.</param>
   /// <param name="t">template.</param>
   /// <returns></returns>
   BOOL ScriptDocument::OnOpenTemplate(Path docPath, const TemplateFile& t)
   {
      WorkerData data(Operation::LoadSaveDocument);
      
      try
      {
         // Feedback
         Console << Cons::UserAction << "Creating script: " << docPath << " from template: " << Path(t.SubPath) << ENDL;
         data.SendFeedback(ProgressType::Operation, 0, VString(L"Creating %s '%s'", t.Name.c_str(), docPath.c_str()));

         // Read/Parse template script
         AppPath path(t.SubPath);
         Script = ScriptFileReader(XFileInfo(path).OpenRead()).ReadFile(path, false);

         // Set properties
         Script.Name = docPath.RemoveExtension().FileName;
         Script.Game = PrefsLib.GameDataVersion;
         Script.Version = 1;
         Script.Description = L"no description";

         // Populate template
         LineArray lines;
         wstring date = COleDateTime(time(nullptr)).Format(L"%d/%m/%Y");
         for (auto& cmd : Script.Commands.Input)
         {
            cmd.Text = cmd.Text.ReplaceAll(L"$NAME$", Script.Name);
            cmd.Text = cmd.Text.ReplaceAll(L"$DATE$", date);
            cmd.Text = cmd.Text.ReplaceAll(L"$AUTHOR$", L"");
            lines.push_back(cmd.Text);
         }

         // Create temp scriptFile
         ScriptFile tmp(Script);
         tmp.FullPath = docPath;

         // Compile changes into temporary script
         ScriptParser parser(tmp, lines, Script.Game);
         parser.Compile();
         
         // Write to disc
         ScriptFileWriter w(XFileInfo(docPath).OpenWrite());
         w.Write(tmp);
         w.Close();

         // Feedback
         data.SendFeedback(Cons::Success, ProgressType::Succcess, 0, L"Script created successfully");
         return TRUE;
      }
      catch (ExceptionBase&  e)
      {
         // Feedback/Display error
         data.SendFeedback(Cons::Error, ProgressType::Failure, 0, VString(L"Failed to create %s '%s'", t.Name.c_str(), docPath.c_str()));
         theApp.ShowError(HERE, e, VString(L"Failed to create %s '%s'", t.Name.c_str(), docPath.c_str()));
         return FALSE;
      }
   }
Exemple #2
0
 /// <summary>Compares descriptor against the specified path.</summary>
 /// <param name="path">The path to compare against</param>
 /// <param name="checkExtension">Whether the file extension must also match</param>
 /// <returns></returns>
 bool  XFileInfo::Matches(Path path, bool checkExtension) const
 {
    return checkExtension ? FullPath == path : Key == path.RemoveExtension(); 
 }