Skip to content

Instantly share code, notes, and snippets.

@tobylane
Created April 6, 2022 21:10
Show Gist options
  • Save tobylane/91f78b5d37be83f5df92deca9797fcbc to your computer and use it in GitHub Desktop.
Save tobylane/91f78b5d37be83f5df92deca9797fcbc to your computer and use it in GitHub Desktop.
The diff from between when I reviewed and the merge. git diff 243efdab53fab362804a71f65a8bada87afe0b3c..84410a86e10ef6e51391d541427bc0cb48fe9f2f CorsixTH/Src/
diff --git a/CorsixTH/Src/config.h.in b/CorsixTH/Src/config.h.in
index 31562afe..2f54ae84 100644
--- a/CorsixTH/Src/config.h.in
+++ b/CorsixTH/Src/config.h.in
@@ -23,7 +23,15 @@ SOFTWARE.
#ifndef CORSIX_TH_CONFIG_H_
#define CORSIX_TH_CONFIG_H_
+/** Interpreter script searching options **/
+// Whether to search in some possible local dirs according to a fixed list,
+// relative to current working dir or program dir
+#cmakedefine CORSIX_TH_SEARCH_LOCAL_DATADIRS
+
+// Name of the script, usually 'CorsixTH.lua'
#cmakedefine CORSIX_TH_INTERPRETER_NAME "@CORSIX_TH_INTERPRETER_NAME@"
+
+// A final path (with file name) to search
#cmakedefine CORSIX_TH_INTERPRETER_PATH "@CORSIX_TH_INTERPRETER_PATH@"
/** Windows Platform SDK usage **/
diff --git a/CorsixTH/Src/main.cpp b/CorsixTH/Src/main.cpp
index 5368453f..a7f25bd2 100644
--- a/CorsixTH/Src/main.cpp
+++ b/CorsixTH/Src/main.cpp
@@ -25,16 +25,9 @@ SOFTWARE.
#include <array>
#include <cstdio>
#include <cstring>
+#include <fstream>
#include <string>
-#ifdef _WIN32
-#include <io.h>
-#define access _access
-#define F_OK 0
-#else
-#include <unistd.h>
-#endif
-
#include "iso_fs.h"
#include "lua.hpp"
#include "lua_rnc.h"
@@ -42,7 +35,7 @@ SOFTWARE.
#include "persist_lua.h"
#include "th_lua.h"
-#ifdef CORSIX_SEARCH_LOCAL_DATADIRS
+#ifdef CORSIX_TH_SEARCH_LOCAL_DATADIRS
#include "../../libs/whereami/whereami.h"
#endif
@@ -65,6 +58,14 @@ inline void preload_lua_package(lua_State* L, const char* name,
fn);
}
+// relace me with C++17 std::filesystem::exists
+inline bool file_exists(const char* f) {
+ std::ifstream file(f);
+ return file.is_open();
+}
+
+inline bool file_exists(const std::string& f) { return file_exists(f.c_str()); }
+
std::string search_script_file(lua_State* L) {
// 1. Check for --interpreter
int iNArgs = lua_gettop(L);
@@ -77,9 +78,9 @@ std::string search_script_file(lua_State* L) {
}
}
-#ifdef CORSIX_SEARCH_LOCAL_DATADIRS
+#ifdef CORSIX_TH_SEARCH_LOCAL_DATADIRS
// 2. Find CorsixTH.lua in working dir and program dir
- static constexpr std::array<const char*, 3> asSearchDirs{
+ static constexpr std::array<const char*, 4> asSearchDirs{
"./",
"CorsixTH/",
"Contents/Resources/",
@@ -91,8 +92,25 @@ std::string search_script_file(lua_State* L) {
if (iProgramPathLength != 0) {
char* sProgramDir = new char[iProgramPathLength + 1];
int iProgramDirLength;
- wai_getExecutablePath(sProgramDir, iProgramPathLength + 1,
- &iProgramDirLength);
+ int iProgramPathLengthReal = wai_getExecutablePath(
+ sProgramDir, iProgramPathLength, &iProgramDirLength);
+ if (iProgramPathLengthReal != iProgramPathLength ||
+ iProgramPathLength >= iProgramDirLength) {
+ if (iProgramPathLengthReal != iProgramPathLength)
+ std::fprintf(stderr,
+ "Path length of CorsixTH binary changed?!?! "
+ "Old: %d, new: %d.\n",
+ iProgramPathLength, iProgramPathLengthReal);
+ else
+ std::fprintf(stderr,
+ "Path to CorsixTH looks like a directory?!?! "
+ "Path is: '%s'.\n",
+ sProgramDir);
+ std::fprintf(stderr, "Please report this incident!\n");
+ std::fflush(stderr);
+ exit(255);
+ }
+ // relace me with C++17 std::filesystem::path::preferred_separator
sProgramDir[iProgramDirLength] = '/';
sProgramDir[iProgramDirLength + 1] = '\0';
strProgramDir = sProgramDir;
@@ -100,20 +118,18 @@ std::string search_script_file(lua_State* L) {
}
}
for (auto sSearchDir : asSearchDirs) {
- std::string strSearchWorkingDir =
+ std::string strPathInWorkingDir =
std::string(sSearchDir) + CORSIX_TH_INTERPRETER_NAME;
- if (access(strSearchWorkingDir.c_str(), F_OK) != -1)
- return strSearchWorkingDir;
+ if (file_exists(strPathInWorkingDir)) return strPathInWorkingDir;
if (!strProgramDir.empty()) {
- std::string strSearchProgramDir = strProgramDir + strSearchWorkingDir;
- if (access(strSearchProgramDir.c_str(), F_OK) != -1)
- return strSearchProgramDir;
+ std::string strPathInProgramDir = strProgramDir + strPathInWorkingDir;
+ if (file_exists(strPathInProgramDir)) return strPathInProgramDir;
}
}
#endif
// 3. Check CORSIX_TH_INTERPRETER_PATH
- if (access(CORSIX_TH_INTERPRETER_PATH, F_OK) != -1)
+ if (file_exists(CORSIX_TH_INTERPRETER_PATH))
return CORSIX_TH_INTERPRETER_PATH;
return "";
@@ -150,9 +166,6 @@ int lua_main_no_eval(lua_State* L) {
// require "debug" (Harmless in Lua 5.1, useful in 5.2 for compatibility)
luaT_execute(L, "require \"debug\"");
- lua_getglobal(L, "assert");
- lua_getglobal(L, "loadfile");
-
auto scriptFilePath = search_script_file(L);
if (scriptFilePath.empty()) {
std::fprintf(stderr,
@@ -161,6 +174,9 @@ int lua_main_no_eval(lua_State* L) {
std::fflush(stderr);
exit(1);
}
+
+ lua_getglobal(L, "assert");
+ lua_getglobal(L, "loadfile");
lua_pushstring(L, scriptFilePath.c_str());
lua_call(L, 1, 2);
diff --git a/CorsixTH/Src/th_gfx_sdl.cpp b/CorsixTH/Src/th_gfx_sdl.cpp
index fbb979bd..aad13c49 100644
--- a/CorsixTH/Src/th_gfx_sdl.cpp
+++ b/CorsixTH/Src/th_gfx_sdl.cpp
@@ -41,10 +41,6 @@ SOFTWARE.
#include "th_map.h"
#if SDL_VERSION_ATLEAST(2, 0, 10)
-
-//! How much to overdraw scaled sprites to ensure no gaps are visible.
-const float frect_overdraw = 0.002f;
-
#define SDL_FRECT_UNIT float
#else
// On older SDL versions, floating point rendering was not available so we fall
@@ -157,10 +153,10 @@ void getScaleRect(const SDL_Rect* rect, double scale_factor,
#if SDL_VERSION_ATLEAST(2, 0, 10)
// If using SDL 2.0.10 or newer, we can use floats to get better precision
// on scaled rendering.
- dst_rect->x = static_cast<float>(rect->x * scale_factor) - frect_overdraw;
- dst_rect->y = static_cast<float>(rect->y * scale_factor) - frect_overdraw;
- dst_rect->w = static_cast<float>(rect->w * scale_factor) + frect_overdraw;
- dst_rect->h = static_cast<float>(rect->h * scale_factor) + frect_overdraw;
+ dst_rect->x = static_cast<float>(rect->x * scale_factor);
+ dst_rect->y = static_cast<float>(rect->y * scale_factor);
+ dst_rect->w = static_cast<float>(rect->w * scale_factor);
+ dst_rect->h = static_cast<float>(rect->h * scale_factor);
#else
// Prior to SDL 2.0.10, fallback to using the enclosing integer SDL_Rect for
// scaled rendering.
diff --git a/CorsixTH/Src/th_movie.cpp b/CorsixTH/Src/th_movie.cpp
index 6ee359c4..60fbd011 100644
--- a/CorsixTH/Src/th_movie.cpp
+++ b/CorsixTH/Src/th_movie.cpp
@@ -24,6 +24,8 @@ SOFTWARE.
#include "config.h"
+#include <libavutil/channel_layout.h>
+
#include "lua_sdl.h"
#if defined(CORSIX_TH_USE_FFMPEG) && defined(CORSIX_TH_USE_SDL_MIXER)
@@ -31,7 +33,6 @@ SOFTWARE.
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
-#include <libavutil/channel_layout.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavutil/opt.h>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment