summaryrefslogtreecommitdiff
path: root/Runtime/Utilities/Argv.cpp
blob: b05da23ef857644dbaefa2ae609a456947beee2d (plain)
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
#include "UnityPrefix.h"
#include "Argv.h"
#if UNITY_WIN
#include "PlatformDependent/Win/WinUtils.h"
#endif

using namespace std;

static int argc;
static const char** argv;
static vector<string> relaunchArguments;

struct KnownArguments
{
	bool isBatchmode;
	bool isAutomated;
};

static KnownArguments knownArgs;

void SetupArgv (int a, const char** b) 
{ 
	argc = a; 
	argv = b; 
	knownArgs.isBatchmode = HasARGV ("batchmode");
	knownArgs.isAutomated = HasARGV ("automated");
}

bool HasARGV (const string& name)
{
	for (int i=0;i<argc;i++)
	{
		if (StrICmp (argv[i], "-" + name) == 0)
			return true;
	}
	return false;
}

bool IsBatchmode ()
{
	return knownArgs.isBatchmode;
}

bool IsHumanControllingUs ()
{
	return !(knownArgs.isBatchmode || knownArgs.isAutomated);
}

void SetIsBatchmode (bool value)
{
	knownArgs.isBatchmode = true;
}

void PrintARGV ()
{
	for (int i=0;i<argc;i++)
	{
		printf_console ("%s\n", argv[i]);
	}
}

vector<string> GetValuesForARGV (const string& name)
{
	vector<string> values;
	values.reserve (argc);
	
	bool found = false;
	for (int i=0;i<argc;i++)
	{
		if (found)
		{
			if (argv[i][0] == '-')
				return values;
			else
				values.push_back (argv[i]);
		}
		else if (StrICmp (argv[i], "-" + name) == 0)
			found = true;
	}

	return values;
}

string GetFirstValueForARGV (const string& name)
{
	vector<string> values = GetValuesForARGV (name);
	if (values.empty ())
		return string ();
	else
		return values[0];
}

vector<string> GetAllArguments()
{
	vector<string> values;
	values.reserve (argc);
	for (int i=1;i<argc;i++)
		values.push_back (argv[i]);
	return values;
}

void SetRelaunchApplicationArguments (const vector<string>& args)
{
	relaunchArguments = args;
}

vector<string> GetRelaunchApplicationArguments ()
{
	return relaunchArguments;
}

void CheckBatchModeErrorString (const string& error)
{
	if (error.empty ())
		return;
	
	ErrorString(error);
	
	if (!IsBatchmode())
		return;
	
#if UNITY_WIN && UNITY_EDITOR
	winutils::RedirectStdoutToConsole();
#elif UNITY_OSX
	ResetStdout();
#endif
	printf_console ("\nAborting batchmode due to failure:\n%s\n\n", error.c_str());
	exit(1);
}