-
Notifications
You must be signed in to change notification settings - Fork 16
/
ParseCmdLine.cpp
175 lines (159 loc) · 5.54 KB
/
ParseCmdLine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <windows.h>
#include "resource.h"
#include "main.h"
#include "mednafen.h"
#include "pcejin.h"
#include "movie.h"
#include "ramwatch.h"
using namespace std;
bool startPaused;
int stateToLoad = -1; //-1 since 0 will be slot 0
//To add additional commandline options
//1) add the identifier (-rom, -play, etc) into the argCmds array
//2) add a variable to store the argument in the list under "Strings that will get parsed"
//3) add an entry in the switch statement in order to assign the variable
//4) add code under the "execute commands" section to handle the given commandline
void ParseCmdLine(LPSTR lpCmdLine, HWND HWnd)
{
string argumentList; //Complete command line argument
argumentList.assign(lpCmdLine); //Assign command line to argumentList
int argLength = argumentList.size(); //Size of command line argument
//List of valid commandline args
string argCmds[] = {"-cfg", "-rom", "-play", "-readwrite", "-loadstate", "-pause", "-lua", ""}; //Hint: to add new commandlines, start by inserting them here.
//Strings that will get parsed:
string CfgToLoad = ""; //Cfg filename
string RomToLoad = ""; //ROM filename
string MovieToLoad = ""; //Movie filename
string StateToLoad = ""; //Savestate filename
vector<string> ScriptsToLoad; //Lua script filenames
string FileToLoad = ""; //Any file
string PauseGame = ""; //adelikat: If user puts anything after -pause it will flag true, documentation will probably say put "1". There is no case for "-paused 0" since, to my knowledge, it would serve no purpose
string ReadWrite = ""; //adelikat: Read Only is the default so this will be the same situation as above, any value will set to read+write status
//Temps for finding string list
int commandBegin = 0; //Beginning of Command
int commandEnd = 0; //End of Command
string newCommand; //Will hold newest command being parsed in the loop
string trunc; //Truncated argList (from beginning of command to end of argumentList
//--------------------------------------------------------------------------------------------
//Commandline parsing loop
for (int x = 0; x < (sizeof argCmds / sizeof string); x++)
{
if (argumentList.find(argCmds[x]) != string::npos)
{
commandBegin = argumentList.find(argCmds[x]) + argCmds[x].size() + (argCmds[x].empty()?0:1); //Find beginning of new command
trunc = argumentList.substr(commandBegin); //Truncate argumentList
commandEnd = trunc.find(" "); //Find next space, if exists, new command will end here
if(argumentList[commandBegin] == '\"') //Actually, if it's in quotes, extend to the end quote
{
commandEnd = trunc.find('\"', 1);
if(commandEnd >= 0)
commandBegin++, commandEnd--;
}
if (commandEnd < 0) commandEnd = argLength; //If no space, new command will end at the end of list
newCommand = argumentList.substr(commandBegin, commandEnd); //assign freshly parsed command to newCommand
}
else
newCommand = "";
//Assign newCommand to appropriate variable
switch (x)
{
case 0: //-cfg
CfgToLoad = newCommand;
break;
case 1: //-rom
RomToLoad = newCommand;
break;
case 2: //-play
MovieToLoad = newCommand;
break;
case 3: //-readwrite
ReadWrite = newCommand;
break;
case 4: //-loadstate
StateToLoad = newCommand;
break;
case 5: //-pause
PauseGame = newCommand;
break;
case 6: //-lua
ScriptsToLoad.push_back(newCommand);
break;
case 7: // (a filename on its own, this must come BEFORE any other options on the commandline)
if(newCommand[0] != '-')
FileToLoad = newCommand;
break;
}
}
//--------------------------------------------------------------------------------------------
//Execute commands
// anything (rom, movie, cfg, luascript, etc.)
//adelikat: for now assume ROM
if (FileToLoad[0])
{
//adelikat: This code is currently in LoadGame, Drag&Drop, Recent ROMs, and here, time for a function
pcejin.romLoaded = true;
pcejin.started = true;
if(!MDFNI_LoadGame(false, FileToLoad.c_str()))
{
pcejin.started = false;
pcejin.romLoaded = false;
}
}
//{
// GensOpenFile(FileToLoad.c_str());
//}
//Cfg
//if (CfgToLoad[0])
//{
// Load_Config((char*)CfgToLoad.c_str(), NULL);
// strcpy(Str_Tmp, "config loaded from ");
// strcat(Str_Tmp, CfgToLoad.c_str());
// Put_Info(Str_Tmp, 2000);
//}
//ROM
if (RomToLoad[0])
{
//adelikat: This code is currently in LoadGame, Drag&Drop, Recent ROMs, and here, time for a function
pcejin.romLoaded = true;
pcejin.started = true;
if(!MDFNI_LoadGame(false, RomToLoad.c_str()))
{
pcejin.started = false;
pcejin.romLoaded = false;
}
}
//Read+Write
bool readwrite = true;
if (ReadWrite[0]) readwrite = false;
//Movie
if (MovieToLoad[0])
{
if (pcejin.romLoaded)
FCEUI_LoadMovie(MovieToLoad.c_str(), readwrite, false, false);
if (AutoRWLoad)
{
//Open Ram Watch if its auto-load setting is checked
OpenRWRecentFile(0);
RamWatchHWnd = CreateDialog(g_hInstance, MAKEINTRESOURCE(IDD_RAMWATCH), g_hWnd, (DLGPROC) RamWatchProc);
}
}
//Loadstate
if (StateToLoad[0])
{
//Load_State((char*)StateToLoad.c_str()); //adelikat: PCEjin doesn't do loadstate as
//adelikat for now let's just load slot 0 no matter what
stateToLoad = 0;
}
//Lua Scripts
//for(unsigned int i = 0; i < ScriptsToLoad.size(); i++)
//{
// if(ScriptsToLoad[i][0])
// {
// const char* error = GensOpenScript(ScriptsToLoad[i].c_str());
// if(error)
// fprintf(stderr, "failed to start script \"%s\" because: %s\n", ScriptsToLoad[i].c_str(), error);
// }
//}
//Paused
if (PauseGame[0]) startPaused = true;
}