1 // Copyright 2014 Wouter van Oortmerssen. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Platform independent file access:
16 typedef int64_t (* FileLoader)(string_view absfilename, string *dest, int64_t start, int64_t len);
17 
18 // Call this at init to determine default folders to load stuff from.
19 string GetMainDirFromExePath(const char *argv_0);
20 // Then pass the result as maindir to InitPlatform.
21 // This also initializes anything else functions in this file need.
22 extern bool InitPlatform(string maindir, const char *auxfilepath, bool from_bundle,
23                              FileLoader loader);
24 extern void AddDataDir(string_view path);  // Any additional dirs besides the above.
25 extern string_view ProjectDir();
26 extern string_view MainDir();
27 
28 extern string_view StripFilePart(string_view filepath);
29 extern const char *StripDirPart(const char *filepath);
30 
31 // Read all or part of a file.
32 // To read the whole file, pass -1 for len.
33 // To just obtain the file length but don't do any reading, pass 0 for len.
34 // Returns file length or read length, or -1 if failed.
35 extern int64_t LoadFile(string_view relfilename, string *dest, int64_t start = 0, int64_t len = -1,
36                         bool binary = true);
37 
38 // fopen based implementation of FileLoader above to pass to InitPlatform if needed.
39 extern int64_t DefaultLoadFile(string_view absfilename, string *dest, int64_t start, int64_t len);
40 
41 extern FILE *OpenForWriting(string_view relfilename, bool binary);
42 extern bool WriteFile(string_view relfilename, bool binary, string_view contents);
43 extern bool FileExists(string_view relfilename);
44 extern bool FileDelete(string_view relfilename);
45 extern string SanitizePath(string_view path);
46 
47 extern void AddPakFileEntry(string_view pakfilename, string_view relfilename, int64_t off,
48                             int64_t len, int64_t uncompressed);
49 
50 extern bool ScanDir(string_view reldir, vector<pair<string, int64_t>> &dest);
51 extern bool ScanDirAbs(string_view absdir, vector<pair<string, int64_t>> &dest);
52 
53 // Logging:
54 
55 enum OutputType {
56     // Temp spam, should eventually be removed, shown only at --debug.
57     OUTPUT_DEBUG,
58     // Output that helps understanding what the code is doing when not under a debugger,
59     // shown with --verbose.
60     OUTPUT_INFO,
61     // Non-critical issues, e.g. SDL errors. This level shown by default.
62     OUTPUT_WARN,
63     // Output by the Lobster code.
64     OUTPUT_PROGRAM,
65     // Compiler & vm errors, program terminates after this. Only thing shown at --silent.
66     OUTPUT_ERROR,
67 };
68 
69 extern OutputType min_output_level;  // Defaults to showing OUTPUT_WARN and up.
70 
71 extern void LogOutput(OutputType ot, const char *buf);
LogOutput(OutputType ot,const string & buf)72 inline void LogOutput(OutputType ot, const string &buf) { LogOutput(ot, buf.c_str()); };
LogOutput(OutputType ot,const Ts &...args)73 template<typename ...Ts> void LogOutput(OutputType ot, const Ts&... args) {
74     if (ot >= min_output_level) LogOutput(ot, cat(args...).c_str());
75 }
76 // This is to make it lazy: arguments are not constructed at all if level is too low.
77 #define LOG_DEBUG(...)   { if (min_output_level <= OUTPUT_DEBUG) \
78                                LogOutput(OUTPUT_DEBUG, __VA_ARGS__); }
79 #define LOG_INFO(...)    { if (min_output_level <= OUTPUT_INFO) \
80                                LogOutput(OUTPUT_INFO, __VA_ARGS__); }
81 #define LOG_WARN(...)    { if (min_output_level <= OUTPUT_WARN) \
82                                LogOutput(OUTPUT_WARN, __VA_ARGS__); }
83 #define LOG_PROGRAM(...) { if (min_output_level <= OUTPUT_PROGRAM) \
84                                LogOutput(OUTPUT_PROGRAM, __VA_ARGS__); }
85 #define LOG_ERROR(...)   { if (min_output_level <= OUTPUT_ERROR) \
86                                LogOutput(OUTPUT_ERROR, __VA_ARGS__); }
87 
88 // Time:
89 extern double SecondsSinceStart();
90 
91 // CPU:
92 extern uint NumHWThreads();
93 extern uint NumHWCores();
94 
95 // Misc:
96 extern void ConditionalBreakpoint(bool shouldbreak);
97 extern void CountingBreakpoint(int i = -1);
98 extern void MakeDPIAware();
99 
100 extern string GetDateTime();
101 
102 extern void SetConsole(bool on);
103 
104 #if defined(__IOS__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__)
105     #define PLATFORM_ES3
106 #endif
107 
108 #if defined(__IOS__) || defined(__ANDROID__)
109     #define PLATFORM_TOUCH
110 #endif
111 
112 #if !defined(PLATFORM_ES3) && !defined(__APPLE__)
113 	#define PLATFORM_WINNIX
114 #endif
115 
116 #if defined(_WIN32) && !defined(SKIP_SDKS)  // FIXME: Also make work on Linux/OS X.
117     #define PLATFORM_VR
118     #define PLATFORM_STEAMWORKS
119 #endif
120