Example #1
0
int StrUtil::StringToInt(const String &s, int def_val)
{
    if (!s.GetCStr())
        return def_val;
    char *stop_ptr;
    int val = strtol(s.GetCStr(), &stop_ptr, 0);
    return (stop_ptr == s.GetCStr() + s.GetLength()) ? val : def_val;
}
Example #2
0
StrUtil::ConversionError StrUtil::StringToInt(const String &s, int &val, int def_val)
{
    val = def_val;
    if (!s.GetCStr())
        return StrUtil::kFailed;
    char *stop_ptr;
    errno = 0;
    long lval = strtol(s.GetCStr(), &stop_ptr, 0);
    if (stop_ptr != s.GetCStr() + s.GetLength())
        return StrUtil::kFailed;
    if (lval > INT_MAX || lval < INT_MIN || errno == ERANGE)
        return StrUtil::kOutOfRange;
    val = (int)lval;
    return StrUtil::kNoError;
}
Example #3
0
void StrUtil::WriteString(const String &s, Stream *out)
{
    size_t len = s.GetLength();
    out->WriteInt32(len);
    if (len > 0)
        out->Write(s.GetCStr(), len);
}
Example #4
0
bool engine_init_gfx_filters(Size &game_size, Size &screen_size, const int color_depth)
{
    Out::FPrint("Initializing gfx filters");
    if (force_gfxfilter[0])
        GfxFilterRequest = force_gfxfilter;
    else
        GfxFilterRequest = usetup.gfxFilterID;
    Out::FPrint("Requested gfx filter: %s", GfxFilterRequest.GetCStr());

    // Try to initialize gfx filter of requested name
    if (GfxFilterRequest.CompareNoCase("max") != 0 &&
        initialize_graphics_filter(GfxFilterRequest, color_depth))
    {
        // Filter found, but we must also try if the engine will be able to set
        // screen resolution
        if (!try_find_nearest_supported_mode(game_size, filter->GetScalingFactor(), screen_size, color_depth,
                usetup.windowed, usetup.prefer_sideborders, usetup.prefer_letterbox))
        {
            delete filter;
            filter = NULL;
        }
    }
    
    // If the filter was not set for any reason, try to choose standard scaling filter
    // of maximal possible scaling factor
    if (!filter)
    {
        String filter_name;
        int scaling_factor;
#if defined (WINDOWS_VERSION) || defined (LINUX_VERSION)
        scaling_factor = try_find_max_supported_uniform_scaling(game_size, screen_size, color_depth,
                            usetup.windowed, usetup.prefer_sideborders, usetup.prefer_letterbox);
        if (scaling_factor == 0)
#endif
        {
            screen_size = game_size;
            scaling_factor = 1;
        }
        filter_name.Format(scaling_factor > 1 ? "StdScale%d" : "None", scaling_factor);
        initialize_graphics_filter(filter_name, color_depth);
    }

    // If not suitable filter still found then return with error message
    if (!filter)
    {
        set_allegro_error("Failed to find acceptable graphics filter");
        return false;
    }

    // On success apply filter and define game frame
    Out::FPrint("Applying graphics filter: %s", filter->GetFilterID());
    gfxDriver->SetGraphicsFilter(filter);    
    game_size.Width = screen_size.Width / filter->GetScalingFactor();
    game_size.Height = screen_size.Height / filter->GetScalingFactor();
    Out::FPrint("Chosen gfx resolution: %d x %d (%d bit), game frame: %d x %d",
        screen_size.Width, screen_size.Height, color_depth, game_size.Width, game_size.Height);
    return true;
}
Example #5
0
bool create_gfx_driver(const String &gfx_driver_id)
{
    Out::FPrint("Init gfx driver: %s", gfx_driver_id.GetCStr());
    if (!pre_create_gfx_driver(gfx_driver_id))
        return false;

    usetup.gfxDriverID = gfxDriver->GetDriverID();
    gfxDriver->SetCallbackOnInit(GfxDriverOnInitCallback);
    gfxDriver->SetTintMethod(TintReColourise);
    return true;
}
Example #6
0
String find_user_cfg_file()
{
    String parent_dir = MakeSpecialSubDir(PathOrCurDir(platform->GetUserConfigDirectory()));
    return String::FromFormat("%s/%s", parent_dir.GetCStr(), DefaultConfigFileName.GetCStr());
}
Example #7
0
String find_user_global_cfg_file()
{
    String parent_dir = PathOrCurDir(platform->GetUserGlobalConfigDirectory());
    return String::FromFormat("%s/%s", parent_dir.GetCStr(), DefaultConfigFileName.GetCStr());
}
Example #8
0
String find_default_cfg_file(const char *alt_cfg_file)
{
    // Try current directory for config first; else try exe dir
    String filename = String::FromFormat("%s/%s", Directory::GetCurrentDirectory().GetCStr(), DefaultConfigFileName.GetCStr());
    if (!Common::File::TestReadFile(filename))
    {
        char conffilebuf[512];
        strcpy(conffilebuf, alt_cfg_file);
        fix_filename_case(conffilebuf);
        fix_filename_slashes(conffilebuf);
        INIgetdirec(conffilebuf, DefaultConfigFileName);
        filename = conffilebuf;
    }
    return filename;
}
Example #9
0
void display_gfx_mode_error(const Size &game_size, const Size &screen_size)
{
    proper_exit=1;
    platform->FinishedUsingGraphicsMode();

    String main_error;
    if (screen_size.IsNull())
        main_error.Format("There was a problem finding appropriate graphics mode for game size %d x %d (%d-bit) and requested filter '%s'.",
            game_size.Width, game_size.Height, firstDepth, GfxFilterRequest.IsEmpty() ? "Undefined" : GfxFilterRequest.GetCStr());
    else
        main_error.Format("There was a problem initializing graphics mode %d x %d (%d-bit) with game size %d x %d and filter '%s'.",
            screen_size.Width, screen_size.Height, firstDepth, game_size.Width, game_size.Height, filter ? filter->GetFilterID() : "Undefined");

    platform->DisplayAlert("%s\n"
            "(Problem: '%s')\n"
            "Try to correct the problem, or seek help from the AGS homepage."
            "%s",
            main_error.GetCStr(), get_allegro_error(), platform->GetGraphicsTroubleshootingText());
}
Example #10
0
void StrUtil::WriteCStr(const String &s, Stream *out)
{
    out->Write(s.GetCStr(), s.GetLength() + 1);
}
Example #11
0
HSaveError OpenSavegameBase(const String &filename, SavegameSource *src, SavegameDescription *desc, SavegameDescElem elems)
{
    UStream in(File::OpenFileRead(filename));
    if (!in.get())
        return new SavegameError(kSvgErr_FileOpenFailed, String::FromFormat("Requested filename: %s.", filename.GetCStr()));

    // Skip MS Windows Vista rich media header
    RICH_GAME_MEDIA_HEADER rich_media_header;
    rich_media_header.ReadFromFile(in.get());

    // Check saved game signature
    bool is_new_save = false;
    size_t pre_sig_pos = in->GetPosition();
    String svg_sig = String::FromStreamCount(in.get(), SavegameSource::Signature.GetLength());
    if (svg_sig.Compare(SavegameSource::Signature) == 0)
    {
        is_new_save = true;
    }
    else
    {
        in->Seek(pre_sig_pos, kSeekBegin);
        svg_sig = String::FromStreamCount(in.get(), SavegameSource::LegacySignature.GetLength());
        if (svg_sig.Compare(SavegameSource::LegacySignature) != 0)
            return new SavegameError(kSvgErr_SignatureFailed);
    }

    SavegameVersion svg_ver;
    SavegameDescription temp_desc;
    HSaveError err;
    if (is_new_save)
        err = ReadDescription(in.get(), svg_ver, temp_desc, desc ? elems : kSvgDesc_None);
    else
        err = ReadDescription_v321(in.get(), svg_ver, temp_desc, desc ? elems : kSvgDesc_None);
    if (!err)
        return err;

    if (src)
    {
        src->Filename = filename;
        src->Version = svg_ver;
        src->InputStream.reset(in.release()); // give the stream away to the caller
    }
    if (desc)
    {
        if (elems & kSvgDesc_EnvInfo)
        {
            desc->EngineName = temp_desc.EngineName;
            desc->EngineVersion = temp_desc.EngineVersion;
            desc->GameGuid = temp_desc.GameGuid;
            desc->GameTitle = temp_desc.GameTitle;
            desc->MainDataFilename = temp_desc.MainDataFilename;
            desc->MainDataVersion = temp_desc.MainDataVersion;
            desc->ColorDepth = temp_desc.ColorDepth;
        }
        if (elems & kSvgDesc_UserText)
            desc->UserText = temp_desc.UserText;
        if (elems & kSvgDesc_UserImage)
            desc->UserImage.reset(temp_desc.UserImage.release());
    }
    return err;
}
Example #12
0
String GetPathInASCII(const String &path)
{
    char ascii_buffer[MAX_PATH];
    if (GetShortPathNameA(path, ascii_buffer, MAX_PATH) == 0)
    {
        Debug::Printf(kDbgMsg_Error, "Unable to determine path: GetShortPathNameA failed.\nArg: %s", path.GetCStr());
        return "";
    }
    return Path::MakeAbsolutePath(ascii_buffer);
}
Example #13
0
String find_game_data_in_directory(const String &path)
{
    String test_file;
    String found_data_file;
    // TODO: find a way to make this platform-agnostic way
    // using find-file interface or something
#if defined (LINUX_VERSION) || defined (MAC_VERSION)
    DIR* fd = NULL;
    struct dirent* entry = NULL;
    version_info_t version_info;

    if ((fd = opendir(path)))
    {
        while ((entry = readdir(fd)))
        {
            // Filename must be >= 4 chars long
            int length = strlen(entry->d_name);
            if (length < 4)
            {
                continue;
            }

            // Exclude the setup program
            if (stricmp(entry->d_name, "winsetup.exe") == 0)
            {
                continue;
            }

            if (stricmp(&(entry->d_name[length - 4]), ".exe") == 0)
            {
                if (!getVersionInformation(entry->d_name, &version_info))
                    continue;
                if (strcmp(version_info.internal_name, "acwin") == 0)
                {
                    test_file.Format("%s/%s", path.GetCStr(), entry->d_name);
                    if (AGS::Common::AssetManager::IsDataFile(test_file))
                    {
                        found_data_file = test_file;
                        break;
                    }
                }
            }
            else if (stricmp(&(entry->d_name[length - 4]), ".ags") == 0 ||
                stricmp(entry->d_name, "ac2game.dat") == 0)
            {
                test_file.Format("%s/%s", path.GetCStr(), entry->d_name);
                if (AGS::Common::AssetManager::IsDataFile(test_file))
                {
                    found_data_file = test_file;
                    break;
                }
            }
        }
        closedir(fd);
    }
#elif defined (WINDOWS_VERSION)
    String path_mask = path;
    path_mask.Append("/*");
    WIN32_FIND_DATAA file_data;
    HANDLE find_handle = FindFirstFileA(path_mask, &file_data);
    if (find_handle != INVALID_HANDLE_VALUE)
    {
        do
        {
            // Filename must be >= 4 chars long
            int length = strlen(file_data.cFileName);
            if (length < 4)
            {
                continue;
            }

            // Exclude the setup program
            if (strcmp(file_data.cFileName, "winsetup.exe") == 0)
            {
                continue;
            }

            if (strcmp(&(file_data.cFileName[length - 4]), ".exe") == 0 ||
                strcmp(&(file_data.cFileName[length - 4]), ".ags") == 0 ||
                strcmp(file_data.cFileName, "ac2game.dat") == 0)
            {
                test_file.Format("%s/%s", path.GetCStr(), file_data.cFileName);
                if (AGS::Common::AssetManager::IsDataFile(test_file))
                {
                    found_data_file = test_file;
                    break;
                }
            }
        }
        while (FindNextFileA(find_handle, &file_data) != FALSE);
        FindClose(find_handle);
    }
#else
    // TODO ??? (PSP, ANDROID)
#endif
    return found_data_file;
}