Advertisement
Redxone

[Doom C++] - Config manager

Dec 23rd, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <fstream>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. bool noconfig = true;
  9. bool nostart = false;
  10.  
  11. bool isFile (string file)
  12. {
  13.     return std::ifstream( (char*) &file[0] );
  14. }
  15.  
  16. int strRename(string oldn, string newn)
  17. {
  18.     return rename( (char*) &oldn[0], (char*) &newn[0]);
  19. }
  20.  
  21. bool isConfigSetup(string path)
  22. {
  23.     return isFile(path + "/gzdoom-" + getenv("USERNAME") + ".ini");
  24. }
  25.  
  26. bool SetupConfig(string cPath)
  27. {
  28.     if(!isFile(cPath)) return 0;
  29.     char* pName = getenv("USERNAME");
  30.     string nPath = "Game/gzdoom-" + (string)pName + ".ini";
  31.     strRename( cPath, nPath );
  32.     return 1;
  33. }
  34.  
  35.  
  36. bool yesno()
  37. {
  38.     cout << "Y/n ?" << endl;
  39.     bool waiting = true;
  40.     bool yes = false;
  41.     while (waiting)
  42.     {
  43.             getch();
  44.             if(GetAsyncKeyState('Y') || GetAsyncKeyState('y'))
  45.             {
  46.                 waiting = false;
  47.                 yes = true;
  48.                 break;
  49.             }
  50.             else if(GetAsyncKeyState('N') || GetAsyncKeyState('n'))
  51.             {
  52.                 waiting = false;
  53.                 break;
  54.             }
  55.     }
  56.  
  57.     return yes;
  58.  
  59. }
  60.  
  61. bool checkConfigName(string name)
  62. {
  63.  if(isFile("Game/gzdoom-" + name + ".ini"))
  64.     {
  65.         cout << "Config found, reset to logged in user?" << endl;
  66.         if(yesno())
  67.         {
  68.             if(!SetupConfig("Game/gzdoom-" + name + ".ini"))
  69.             {
  70.                 cout << "Failed. " << endl;
  71.                 return 0;
  72.             }
  73.             cout << "Success!" << endl;
  74.             return 1;
  75.         }
  76.  
  77.     }
  78.     else
  79.     {
  80.         cout << "Config by name: " << "Game/gzdoom-" << name << ".ini" << " not found." << endl;
  81.         return 0;
  82.     }
  83. }
  84.  
  85. bool ManualConfig()
  86. {
  87.     string mPath;
  88.     cout << "Please find your config in [Game/gzdoom-[name].ini]: ";
  89.     cin >> mPath;
  90.  
  91.     return checkConfigName(mPath);
  92. }
  93.  
  94. bool AskConfig()
  95. {
  96.         cout << "Would you like to manually enter a Config name" << endl;
  97.         if(yesno() == true)
  98.         {
  99.             if (!ManualConfig())
  100.             {
  101.                 return AskConfig();
  102.             }
  103.                 cout << "Starting Reveil..." << endl;
  104.                 return 1;
  105.         }
  106.         else
  107.         {
  108.             if(noconfig)
  109.             {
  110.                 cout << "A config wasnt found.. attempt to start?" << endl;
  111.                 if(!yesno())
  112.                 {
  113.                     nostart=true;
  114.                     return 0;
  115.                 }
  116.             }
  117.  
  118.                 cout << "Attempting to start with fresh config..." << endl;
  119.                 return 1;
  120.         }
  121.  
  122.  
  123. }
  124.  
  125. bool DetectConfig(string path)
  126. {
  127.     char* pName = getenv("USERNAME");
  128.     cout << "Detected: " << pName << endl;
  129.     cout << "Preparing config for " << pName << endl;
  130.     string nPath;
  131.     if(isFile( path + "/gzdoom-NAME.ini" ))
  132.     {
  133.         nPath = path + "/gzdoom-" + (string)pName + ".ini";
  134.         strRename(path + "/gzdoom-NAME.ini", nPath );
  135.         noconfig = false;
  136.         return 1;
  137.     }
  138.     else if(!isConfigSetup("Game/"))
  139.     {
  140.         cout << "Config missing." << endl;
  141.         AskConfig();
  142.         return 1;
  143.     }
  144. }
  145.  
  146. int main()
  147. {
  148.     char* pName;
  149.     cout << "Starting GZDoom with Reveil.. " << endl;
  150.     cout << "Preparing config.. " << endl;
  151.  
  152.     if( DetectConfig("Game") )
  153.     {
  154.         if(!nostart) WinExec("Game/gzdoom.exe -file Game/ReveilBundle.pk3 -file jenesis.wad",SW_NORMAL);
  155.     }
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement