44using namespace geode ::prelude;
55
66// Loads the file into a std::vector<string>, returns false if it fails.
7- bool SplashRead::loadFile ()
7+ bool SplashRead::loadFile (std::filesystem::path filePath )
88{
9+ if (filePath.empty ()) return false ;
10+
11+ m_FileStream.open (filePath);
912 if (!m_FileStream.is_open ())
1013 {
11- log::error (" Attempting to load a file from unopened stream !" );
14+ log::error (" File failed to open! May not exist !" );
1215 return false ;
1316 }
1417
@@ -45,53 +48,51 @@ bool SplashRead::loadFile()
4548 continue ;
4649 }
4750 }
51+
52+ // Replace __LINE__ with the line number
53+ std::string lineMacro = " __LINE__" ;
54+ if (size_t pos = line.find (lineMacro);
55+ pos != std::string::npos)
56+ {
57+ line.replace (pos, lineMacro.length (), std::to_string (lineNum));
58+ }
59+
60+ // Replace __PLAYER_USERNAME__ with player name
61+ std::string playerMacro = " __PLAYER_USERNAME__" ;
62+ if (size_t pos = line.find (playerMacro);
63+ pos != std::string::npos)
64+ {
65+ line.replace (pos, playerMacro.length (), GJAccountManager::get ()->m_username );
66+ }
4867
49- // Non-alphanumeric characters are not allowed, if we see one, we fail.
5068 // Algorithm provided by undefined06855 on Discord and Git
69+ std::string trimmedLine;
70+ trimmedLine.reserve (line.length ());
5171 for (int i = 0 ; i < line.length (); i++)
5272 {
53- if (line[i] == ' \0 ' ) continue ;
54- if (line[i] < ' ' || line[i] > ' ~' )
55- {
56- if (line[i] != ' \n ' && line[i] != ' \r ' )
57- {
58- log::error (" Non-alphanumeric character detected at col {} of line {}" , i + 1 , lineNum);
59- return false ;
60- }
61- }
73+ if (line[i] == ' \0 ' || line[i] == ' \n ' || line[i] == ' \r ' || line[i] < ' ' || line[i] > ' ~' ) continue ;
74+ trimmedLine += line[i];
6275 }
6376
6477 // If our file stream fails during loading then we gotta return false
6578 if (m_FileStream.fail ())
6679 {
6780 log::error (" File stream failed during load." );
81+ m_FileStream.close ();
6882 return false ;
6983 }
7084
7185 // Finally we can push back our processed string
72- m_Lines.push_back (line );
86+ m_Lines.push_back (trimmedLine );
7387 lineNum++;
7488 }
7589
90+ m_FileStream.close ();
7691 return true ;
7792}
7893
79- SplashRead::SplashRead (std::string filePath )
94+ SplashRead::SplashRead ()
8095{
81- m_FileStream.open (Mod::get ()->getResourcesDir () / filePath);
82- if (m_FileStream.fail ())
83- {
84- log::error (" Failed to open file {}" , Mod::get ()->getResourcesDir () / filePath);
85- m_FileStream.close ();
86- }
87-
88- if (!loadFile ())
89- {
90- log::error (" Failed to load file {}" , filePath);
91- m_Lines.clear ();
92- }
93-
94- m_FileStream.close ();
9596}
9697
9798std::string SplashRead::getRandomLine ()
@@ -105,5 +106,17 @@ std::string SplashRead::getRandomLine()
105106 std::random_device rd;
106107 std::mt19937 gen (rd ());
107108 std::uniform_int_distribution<> dis (0 , m_Lines.size () - 1 );
108- return m_Lines[dis (gen)];
109+
110+ std::string selected = m_Lines[dis (gen)];
111+ std::string splashText (selected); // Copy to avoid modifying original
112+
113+ std::string randMacro = " __RANDOM__" ;
114+ if (size_t pos = splashText.find (randMacro);
115+ pos != std::string::npos)
116+ {
117+ std::uniform_int_distribution<> dis0_100 (0 , 100 );
118+ splashText.replace (pos, randMacro.length (), std::to_string (dis0_100 (gen)));
119+ }
120+
121+ return splashText;
109122}
0 commit comments