Skip to content

Commit

Permalink
Merge pull request #1448 from dgelessus/hsstream_raii
Browse files Browse the repository at this point in the history
Close `hsStream`s automatically in destructors
  • Loading branch information
Hoikas authored Aug 13, 2023
2 parents 9956967 + ae249da commit 2958319
Show file tree
Hide file tree
Showing 59 changed files with 486 additions and 612 deletions.
66 changes: 26 additions & 40 deletions Sources/Plasma/Apps/plClient/plClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,6 @@ bool plClient::MsgReceive(plMessage* msg)
char buf[256];
sprintf(buf, "%s %d\n", plAgeLoader::GetInstance()->GetCurrAgeFilename(), fNumPostLoadMsgs);
s.WriteString(buf);
s.Close();
#endif
#endif
}
Expand Down Expand Up @@ -2021,9 +2020,6 @@ void plClient::IDetectAudioVideoSettings()

plPipeline::fDefaultPipeParams.VSync = false;

int val = 0;
hsStream *stream = nullptr;
hsUNIXStream s;
plFileName audioIniFile = plFileName::Join(plFileSystem::GetInitPath(), "audio.ini");
plFileName graphicsIniFile = plFileName::Join(plFileSystem::GetInitPath(), "graphics.ini");

Expand All @@ -2036,54 +2032,44 @@ void plClient::IDetectAudioVideoSettings()
#endif

//check to see if audio.ini exists
if (s.Open(audioIniFile))
s.Close();
else
if (!plFileInfo(audioIniFile).Exists())
IWriteDefaultAudioSettings(audioIniFile);

// check to see if graphics.ini exists
if (s.Open(graphicsIniFile))
s.Close();
else
if (!plFileInfo(graphicsIniFile).Exists())
IWriteDefaultGraphicsSettings(graphicsIniFile);
}

void plClient::IWriteDefaultAudioSettings(const plFileName& destFile)
{
hsStream *stream = plEncryptedStream::OpenEncryptedFileWrite(destFile);
WriteBool(stream, "Audio.Initialize", true);
WriteBool(stream, "Audio.UseEAX", false);
WriteInt(stream, "Audio.SetPriorityCutoff", 6);
WriteInt(stream, "Audio.MuteAll", false);
WriteInt(stream, "Audio.SetChannelVolume SoundFX", 1);
WriteInt(stream, "Audio.SetChannelVolume BgndMusic", 1);
WriteInt(stream, "Audio.SetChannelVolume Ambience", 1);
WriteInt(stream, "Audio.SetChannelVolume NPCVoice", 1);
WriteInt(stream, "Audio.EnableVoiceRecording", 1);
WriteInt(stream, "Audio.EnableSubtitles", true);
stream->Close();
delete stream;
stream = nullptr;
std::unique_ptr<hsStream> stream = plEncryptedStream::OpenEncryptedFileWrite(destFile);
WriteBool(stream.get(), "Audio.Initialize", true);
WriteBool(stream.get(), "Audio.UseEAX", false);
WriteInt(stream.get(), "Audio.SetPriorityCutoff", 6);
WriteInt(stream.get(), "Audio.MuteAll", false);
WriteInt(stream.get(), "Audio.SetChannelVolume SoundFX", 1);
WriteInt(stream.get(), "Audio.SetChannelVolume BgndMusic", 1);
WriteInt(stream.get(), "Audio.SetChannelVolume Ambience", 1);
WriteInt(stream.get(), "Audio.SetChannelVolume NPCVoice", 1);
WriteInt(stream.get(), "Audio.EnableVoiceRecording", 1);
WriteInt(stream.get(), "Audio.EnableSubtitles", true);
}

void plClient::IWriteDefaultGraphicsSettings(const plFileName& destFile)
{
hsStream *stream = plEncryptedStream::OpenEncryptedFileWrite(destFile);

WriteInt(stream, "Graphics.Width", plPipeline::fDefaultPipeParams.Width);
WriteInt(stream, "Graphics.Height", plPipeline::fDefaultPipeParams.Height);
WriteInt(stream, "Graphics.ColorDepth", plPipeline::fDefaultPipeParams.ColorDepth);
WriteBool(stream, "Graphics.Windowed", plPipeline::fDefaultPipeParams.Windowed);
WriteInt(stream, "Graphics.AntiAliasAmount", plPipeline::fDefaultPipeParams.AntiAliasingAmount);
WriteInt(stream, "Graphics.AnisotropicLevel", plPipeline::fDefaultPipeParams.AnisotropicLevel );
WriteInt(stream, "Graphics.TextureQuality", plPipeline::fDefaultPipeParams.TextureQuality);
WriteInt(stream, "Quality.Level", plPipeline::fDefaultPipeParams.VideoQuality);
WriteInt(stream, "Graphics.Shadow.Enable", plPipeline::fDefaultPipeParams.Shadows);
WriteInt(stream, "Graphics.EnablePlanarReflections", plPipeline::fDefaultPipeParams.PlanarReflections);
WriteBool(stream, "Graphics.EnableVSync", plPipeline::fDefaultPipeParams.VSync);
stream->Close();
delete stream;
stream = nullptr;
std::unique_ptr<hsStream> stream = plEncryptedStream::OpenEncryptedFileWrite(destFile);

WriteInt(stream.get(), "Graphics.Width", plPipeline::fDefaultPipeParams.Width);
WriteInt(stream.get(), "Graphics.Height", plPipeline::fDefaultPipeParams.Height);
WriteInt(stream.get(), "Graphics.ColorDepth", plPipeline::fDefaultPipeParams.ColorDepth);
WriteBool(stream.get(), "Graphics.Windowed", plPipeline::fDefaultPipeParams.Windowed);
WriteInt(stream.get(), "Graphics.AntiAliasAmount", plPipeline::fDefaultPipeParams.AntiAliasingAmount);
WriteInt(stream.get(), "Graphics.AnisotropicLevel", plPipeline::fDefaultPipeParams.AnisotropicLevel );
WriteInt(stream.get(), "Graphics.TextureQuality", plPipeline::fDefaultPipeParams.TextureQuality);
WriteInt(stream.get(), "Quality.Level", plPipeline::fDefaultPipeParams.VideoQuality);
WriteInt(stream.get(), "Graphics.Shadow.Enable", plPipeline::fDefaultPipeParams.Shadows);
WriteInt(stream.get(), "Graphics.EnablePlanarReflections", plPipeline::fDefaultPipeParams.PlanarReflections);
WriteBool(stream.get(), "Graphics.EnableVSync", plPipeline::fDefaultPipeParams.VSync);
}


Expand Down
4 changes: 1 addition & 3 deletions Sources/Plasma/Apps/plClient/win32/winmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,10 +875,8 @@ INT_PTR CALLBACK UruLoginDialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPA
{
plFileName gipath = plFileName::Join(plFileSystem::GetInitPath(), "general.ini");
ST::string ini_str = ST::format("App.SetLanguage {}\n", plLocalization::GetLanguageName(new_language));
hsStream* gini = plEncryptedStream::OpenEncryptedFileWrite(gipath);
std::unique_ptr<hsStream> gini = plEncryptedStream::OpenEncryptedFileWrite(gipath);
gini->WriteString(ini_str);
gini->Close();
delete gini;
}

memset(&pLoginParam->authError, 0, sizeof(pLoginParam->authError));
Expand Down
1 change: 0 additions & 1 deletion Sources/Plasma/Apps/plFileSecure/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ void GenerateKey(bool useDefault)
hsUNIXStream out;
out.Open(plSecureStream::kKeyFilename, "wb");
out.Write(sizeof(uint32_t) * std::size(key), (void*)key);
out.Close();
}

void SecureFiles(const plFileName& dir, const ST::string& ext, uint32_t* key)
Expand Down
20 changes: 11 additions & 9 deletions Sources/Plasma/Apps/plPageInfo/plPageInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#include "hsStream.h"
#include "hsTimer.h"

#include <memory>
#include <string_theory/format>

#include "pnFactory/plFactory.h"
Expand Down Expand Up @@ -212,7 +213,7 @@ class plStatDumpIterator : public plRegistryPageIterator, public plRegistryKeyIt
{
protected:
plFileName fOutputDir;
hsUNIXStream fStream;
std::unique_ptr<hsFileSystemStream> fStream;

public:
plStatDumpIterator(const plFileName& outputDir) : fOutputDir(outputDir) {}
Expand All @@ -221,16 +222,16 @@ class plStatDumpIterator : public plRegistryPageIterator, public plRegistryKeyIt
{
const plKeyImp* imp = plKeyImp::GetFromKey(key);

fStream.WriteString(key->GetName());
fStream.WriteString(",");
fStream->WriteString(key->GetName());
fStream->WriteString(",");

fStream.WriteString(plFactory::GetNameOfClass(key->GetUoid().GetClassType()));
fStream.WriteString(",");
fStream->WriteString(plFactory::GetNameOfClass(key->GetUoid().GetClassType()));
fStream->WriteString(",");

char buf[30];
sprintf(buf, "%u", imp->GetDataLen());
fStream.WriteString(buf);
fStream.WriteString("\n");
fStream->WriteString(buf);
fStream->WriteString("\n");

return true;
}
Expand All @@ -241,12 +242,13 @@ class plStatDumpIterator : public plRegistryPageIterator, public plRegistryKeyIt

plFileName fileName = plFileName::Join(fOutputDir,
ST::format("{}_{}.csv", info.GetAge(), info.GetPage()));
fStream.Open(fileName, "wt");
fStream = std::make_unique<hsUNIXStream>();
fStream->Open(fileName, "wt");

page->LoadKeys();
page->IterateKeys(this);

fStream.Close();
fStream.reset();

return true;
}
Expand Down
3 changes: 0 additions & 3 deletions Sources/Plasma/Apps/plPageOptimizer/plPageOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,5 @@ void plPageOptimizer::IRewritePage()
newPage.WriteLE32(dataLen);
}
}

newPage.Close();
oldPage.Close();
}
}
57 changes: 27 additions & 30 deletions Sources/Plasma/Apps/plPythonPack/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,6 @@ void WritePythonFile(const plFileName &fileName, const plFileName &path, hsStrea
delete [] code;
Py_XDECREF(pythonCode);
Py_XDECREF(fModule);

pyStream.Close();
glueStream.Close();
}

void FindFiles(std::vector<plFileName> &filenames, std::vector<plFileName> &pathnames, const plFileName& path)
Expand Down Expand Up @@ -291,40 +288,40 @@ void PackDirectory(const plFileName& dir, const plFileName& rootPath, const plFi


// ok, we know how many files we're gonna pack, so make a fake index (we'll fill in later)
hsUNIXStream s;
if (!s.Open(pakName, "wb"))
return;

s.WriteLE32((uint32_t)fileNames.size());
for (const plFileName& fn : fileNames)
{
s.WriteSafeString(fn.AsString());
s.WriteLE32(0);
}
hsUNIXStream s;
if (!s.Open(pakName, "wb"))
return;

PythonInterface::initPython(rootPath, extraDirs, out, stderr);

std::vector<uint32_t> filePositions;
filePositions.resize(fileNames.size());
s.WriteLE32((uint32_t)fileNames.size());
for (const plFileName& fn : fileNames)
{
s.WriteSafeString(fn.AsString());
s.WriteLE32(0);
}

for (size_t i = 0; i < fileNames.size(); i++)
{
// strip '.py' from the file name
plFileName properFileName = fileNames[i].StripFileExt();
uint32_t initialPos = s.GetPosition();
WritePythonFile(properFileName, pathNames[i], &s);
PythonInterface::initPython(rootPath, extraDirs, out, stderr);

std::vector<uint32_t> filePositions(fileNames.size());

filePositions[i] = initialPos;
}
for (size_t i = 0; i < fileNames.size(); i++)
{
// strip '.py' from the file name
plFileName properFileName = fileNames[i].StripFileExt();
uint32_t initialPos = s.GetPosition();
WritePythonFile(properFileName, pathNames[i], &s);

filePositions[i] = initialPos;
}

s.SetPosition(sizeof(uint32_t));
for (size_t i = 0; i < fileNames.size(); i++)
{
s.WriteSafeString(fileNames[i].AsString());
s.WriteLE32(filePositions[i]);
s.SetPosition(sizeof(uint32_t));
for (size_t i = 0; i < fileNames.size(); i++)
{
s.WriteSafeString(fileNames[i].AsString());
s.WriteLE32(filePositions[i]);
}
}

s.Close();
ST::printf(out, "\nPython Package written to {}\n", pakName);

PythonInterface::finiPython();
Expand Down
Loading

0 comments on commit 2958319

Please sign in to comment.