Search code examples
visual-c++fstreamwix3

fstream cannot open file installed by Wix toolset 3


Recently I have created an installer using Wix Toolset 3. What it does is it installs png and json files onto the user's PC, as well as an exe file for a game I've been working on that requires all of the pngs and the json file to work properly. The program uses fstream to read the json file and convert the numbers inside of it to tiles for the map system. However, when the user installs my program using the installer, my program is unable to read the file. I have verified it using an if statement to test if the file can be opened, and if it can't, to not even attempt to generate a map. Also, if the user copies the json file they get, pastes it somewhere else, remove the original one that the installer put on their PC, and replaces it with the one the copied, the program is able to open the file. Here is the code from the map reading class (Map.cpp and Map.h) and the code for the installer (Product.wxs):

(In Map.h):

#pragma once
#include <string>

class Map {
public:

    Map();
    ~Map();

    static void LoadMap(std::string path, int sizeX, int sizeY, int layers);
};

(In Map.cpp):

#include "Map.h"
#include "Game.h"
#include <fstream>

Map::Map() {
}

Map::~Map() {
}

void Map::LoadMap(std::string path, int sizeX, int sizeY, int layers) {
    char tile;
    std::fstream mapFile;
    mapFile.open(path);
    if (mapFile.is_open()) {
        for (int l = 0; l < layers; l++) {
            for (int y = 0; y < sizeY; y++) {
                for (int x = 0; x < sizeX; x++) {
                    mapFile.get(tile);
                    Game::AddTile(atoi(&tile), x * 64, y * 64);
                    mapFile.ignore();
                }
            }
        }
    }
    mapFile.close();
}

(In Product.wxs):

<?xm version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
  xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
    <Product Id="fb88098c-8720-4f04-bb0d-38432fb62c1b" 
           Name="Ophicyte" 
           Language="1033" 
           Version="0.0.0.1" 
           Manufacturer="U-Bit Company" 
           UpgradeCode="49eaae97-93b9-4e34-ab73-d21c5e3b74d1">

    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

        <MajorUpgrade DowngradeErrorMessage="A newer version of Ophicyte is already installed." />
        <MediaTemplate />

        <Feature Id="ProductFeature" Title="Ophicyte" Level="1">
            <ComponentGroupRef Id="ProductComponents" />
        </Feature>
    <Icon Id="mainicon.ico" SourceFile="..\UBitEngine\assets\mainicon.ico"/>
  <Feature Id="Uninstall">
    <ComponentRef Id="UninstallFolder" Primary="yes"/>
  </Feature>
    </Product>

    <Fragment>


    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="DesktopFolder" Name="Desktop" >
        <Component Id="OphicyteDesktopShortcut" Guid="95ba12b1-e9d7-467e-9008-e46630122a1a">

        <Shortcut Id="OphicyteShortcutDesktop"
                  Name="Ophicyte"
                  Target="$(var.UBitEngine.TargetFileName)"
                  WorkingDirectory="TARGETDIR"
                  Icon="mainicon.ico"
                  IconIndex="0"
                  Advertise="no"/>
          <RemoveFolder Id="DesktopFolder" On="uninstall"/>

           <RegistryValue
             Root="HKCU"
             Key="Software/Ophicyte"
             Name="installed"
             Type="integer"
             Value="1"
             KeyPath="yes"/>
          </Component>
      </Directory>

        <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="Ophicyte" >
          <Directory Id="ASSETSDIR" Name="assets">
            <Component Id="ASSETSFOLDER" Guid="c18af068-440c-4faf-8fb0-22da79ddfa53"
                         SharedDllRefCount="no" KeyPath="no" NeverOverwrite="no" Permanent="no" Transitive="no" Win64="no" Location="either">
              <RemoveFolder Id="ASSETSFOLDER" Directory="ASSETSDIR" On="uninstall"/>
              <CreateFolder/>
            </Component>
            <!--Icons:-->
            <Component Id='mainicon' Guid='a4788915-ff9a-4244-a330-d4084fa3aa59'>
              <File Id='mainicon' Name='mainicon.ico' Source='..\UBitEngine\assets\mainicon.ico' KeyPath='yes'/>
            <!--Sprites:-->
            </Component>
            <Component Id="CharacterFemale" Guid="f9e432c2-f74f-49ae-bb0c-e27d7184f578">
              <File Id='CharacterFemale' Name='Character(female).png' Source="..\UBitEngine\assets\Character(female).png" KeyPath='yes' />
            </Component>
            <Component Id='Character' Guid='52a62880-5e56-4128-94e9-38fd9b7908c9'>
              <File Id='Character' Name='Character.png' Source='..\UBitEngine\assets\Character.png' KeyPath='yes'/>
            </Component>
            <Component Id='GrassTufts1' Guid='24241c90-0af1-417c-984a-9e6d18d22315'>
              <File Id='GrassTufts1' Name='Grass Tufts1.png' Source='..\UBitEngine\assets\Grass Tufts1.png' KeyPath='yes'/>
            </Component>
            <Component Id='Grass1' Guid='aab9b2fa-4583-4d11-b355-09e09cd20f63'>
              <File Id='Grass1' Name='Grass1.png' Source='..\UBitEngine\assets\Grass1.png' KeyPath='yes'/>
            </Component>
            <Component Id='Ocean1' Guid='aae24e29-3eba-4880-9242-8d29fc2559e9'>
              <File Id='Ocean1' Name='Ocean1.png' Source='..\UBitEngine\assets\Ocean1.png' KeyPath='yes'/>
            </Component>
            <Component Id='Tree2night' Guid='c9493e18-2814-408d-b750-15127b218d44'>
              <File Id='Tree2night' Name='Tree2(night).png' Source='..\UBitEngine\assets\Tree2(night).png' KeyPath='yes'/>
            </Component>
            <Component Id='Tree2' Guid='75e4804c-d96f-4190-a411-06affe9e9ad9'>
              <File Id='Tree2' Name='Tree2.png' Source='..\UBitEngine\assets\Tree2.png' KeyPath='yes'/>
            </Component>
            <!--Sound effects:-->
            <Component Id='Slash3' Guid='03a64a3b-6234-4db8-b4e7-970a1d557979'>
              <File Id='Slash3' Name='Slash3.wav' Source='..\UBitEngine\assets\Slash3.wav' KeyPath='yes'/>
            </Component>
            <!--Maps:-->
            <Component Id='Overworld' Guid='cc8bb3a0-b0a3-48f2-8df6-2b7c5d96aa2d'>
              <File Id='Overworld' Name='Overworld.json' Source='..\UBitEngine\assets\Overworld.json' KeyPath='yes'/>
            </Component>
          </Directory>
        </Directory>
      </Directory>
    </Directory>
      <!--Features for icons:-->
      <Feature Id="CREATEMAINICON" Title="mainicon.ico" Level="1">
        <ComponentRef Id="mainicon"/>
      </Feature>
      <!--Features for shortcuts:-->
      <Feature Id='SHORTCUT' Title='Ophicyte' Level='1'>
        <ComponentRef Id='OphicyteDesktopShortcut'/>
      </Feature>
      <!--Features for folders:-->
    <Feature Id="CREATEASSETSFOLDER" Title="assets" Level="1">
      <ComponentRef Id="ASSETSFOLDER"/>
    </Feature>
      <!--Features for sprites:-->
    <Feature Id="CREATECHARACTERFEMALE" Title="Character(female).png" Level="1">
      <ComponentRef Id="CharacterFemale"/>
    </Feature>
      <Feature Id="CREATECHARACTER" Title="Character.png" Level="1">
        <ComponentRef Id="Character"/>
      </Feature>
      <Feature Id="CREATEGRASSTUFTS1" Title="Grass Tufts1.png" Level="1">
        <ComponentRef Id="GrassTufts1"/>
      </Feature>
      <Feature Id="CREATEGRASS1" Title="Grass1.png" Level="1">
        <ComponentRef Id="Grass1"/>
      </Feature>
      <Feature Id="CREATEOCEAN1" Title="Ocean1.png" Level="1">
        <ComponentRef Id="Ocean1"/>
      </Feature>
      <Feature Id="CREATETREE2NIGHT" Title="Tree2(night).png" Level="1">
        <ComponentRef Id="Tree2night"/>
      </Feature>
      <Feature Id="CREATETREE2" Title="Tree2.png" Level="1">
        <ComponentRef Id="Tree2"/>
      </Feature>
    <!--Features for sound effects:-->
    <Feature Id="CREATESLASH3" Title="Slash3.wav" Level="1">
      <ComponentRef Id="Slash3"/>
    </Feature>
    <!--Features for maps:-->
    <Feature Id="CREATEOVERWORLD" Title="Overworld.json" Level="1">
      <ComponentRef Id="Overworld"/>
    </Feature>
  </Fragment>

  <Fragment>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
      <Component Id="ProductComponent">
        <File Source="$(var.UBitEngine.TargetPath)" />
      </Component>
    <Component Id="libpng" Guid="60fb99cf-5a5f-4dc2-8928-9c0b5232d046">        
            <File Id='libpng' Name='libpng16-16.dll' Source="..\UBitEngine\libpng16-16.dll" KeyPath='yes' />
        </Component>
    <Component Id="SDL2" Guid="9ee9481a-d57d-499d-8732-574ac6338980">        
            <File Id='SDL2' Name='SDL2.dll' Source="..\UBitEngine\SDL2.dll" KeyPath='yes' />
        </Component>
    <Component Id="SDL2image" Guid="bc104401-71d7-4bbd-a2fc-00f7b31045c9">        
            <File Id='SDL2image' Name='SDL2_image.dll' Source="..\UBitEngine\SDL2_image.dll" KeyPath='yes' />
        </Component>
      <Component Id='zlib' Guid='a9732a61-c65f-4cfc-ac82-48da7d2f9808'>
        <File Id='zlib' Name='zlib1.dll' Source='..\UBitEngine\zlib1.dll' KeyPath='yes'/>
      </Component>
    </ComponentGroup>
  </Fragment>
  <Fragment>
    <ComponentGroup Id="Assets" Directory="ASSETSFOLDER"/>
  </Fragment>
  <Fragment>
    <Component Id="NEWCOMP" Guid="6b94d818-8cbc-45c7-8ded-37fc8048cc71" Directory="INSTALLFOLDER">
      <RemoveFile Id="RemoveInstallFolder2" Name="Ophicyte" On="uninstall" />
    </Component>
  </Fragment>
<Fragment Id="FolderUninstall">
  <?define RegDir="SYSTEM\ControlSet001\services\[UBitEngine]:[Ophicyte]"?>
  <?define RegValueName=InstallDir?>
<Property Id="INSTALLFOLDER">
  <RegistrySearch Root="HKLM" Key="$(var.RegDir)" Type="raw" Id="APPLICATIONFOLDER_REGSEARCH" Name="$(var.RegValueName)" />
</Property>
<DirectoryRef Id="INSTALLFOLDER">
  <Component Id="UninstallFolder" Guid="4e61c6b1-fe6f-4cbb-8000-b94cf5f333d8">
    <CreateFolder Directory="INSTALLFOLDER"/>
  <util:RemoveFolderEx Property="INSTALLFOLDER" On="uninstall"/>
    <RemoveFolder Id="INSTALLFOLDER" On="uninstall"/>
  <RegistryValue Root="HKLM" Key="$(var.RegDir)" Name="$(var.RegValueName)" Type="string" Value="[INSTALLFOLDER]" KeyPath="yes"/>
  </Component>
</DirectoryRef>
</Fragment>
</Wix>

Also, the value passed in the LoadMap function for path is "assets/Overworld.json". I am certain that the problem is not caused by not having the correct references or not giving the user all of the necessary files. If anyone has any idea how to fix this, your help would be greatly appreciated. Also, Overworld.json looks exactly like this:

3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,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3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3

Solution

  • (Quote from Igor Tandetnik):

    "Replace fstream with ifstream. The former attempts to open the file for both reading and writing by default. But directories under C:\Program Files are non-writable by non-admins by default, for security reasons. That's why your program works when copied elsewhere."