-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix random seed not being used bug (fixes #118)
- Loading branch information
Showing
10 changed files
with
255 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ cdogs-sdl | |
*.exe | ||
cdogs-sdl-editor | ||
libcdogs.a | ||
autosave_test | ||
config_test | ||
tmp | ||
libjson.a | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
C-Dogs SDL | ||
A port of the legendary (and fun) action/arcade cdogs. | ||
Copyright (c) 2013, Cong Xu | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
#include "autosave.h" | ||
|
||
#include <locale.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include <json/json.h> | ||
|
||
#include <cdogs/utils.h> | ||
|
||
|
||
void AutosaveInit(Autosave *autosave) | ||
{ | ||
strcpy(autosave->LastMission.CampaignPath, ""); | ||
strcpy(autosave->LastMission.Password, ""); | ||
} | ||
|
||
static void LoadLastMissionNode(MissionSave *lm, json_t *node) | ||
{ | ||
strcpy(lm->CampaignPath, json_find_first_label(node, "CampaignPath")->child->text); | ||
strcpy(lm->Password, json_find_first_label(node, "Password")->child->text); | ||
} | ||
static void AddLastMissionNode(MissionSave *lm, json_t *root) | ||
{ | ||
json_t *subConfig = json_new_object(); | ||
json_insert_pair_into_object( | ||
subConfig, "CampaignPath", json_new_string(lm->CampaignPath)); | ||
json_insert_pair_into_object( | ||
subConfig, "Password", json_new_string(lm->Password)); | ||
json_insert_pair_into_object(root, "LastMission", subConfig); | ||
} | ||
|
||
void AutosaveLoad(Autosave *autosave, const char *filename) | ||
{ | ||
FILE *f = fopen(filename, "r"); | ||
json_t *root = NULL; | ||
|
||
if (f == NULL) | ||
{ | ||
printf("Error loading autosave '%s'\n", filename); | ||
goto bail; | ||
} | ||
|
||
if (json_stream_parse(f, &root) != JSON_OK) | ||
{ | ||
printf("Error parsing autosave '%s'\n", filename); | ||
goto bail; | ||
} | ||
LoadLastMissionNode(&autosave->LastMission, json_find_first_label(root, "LastMission")->child); | ||
|
||
bail: | ||
json_free_value(&root); | ||
if (f != NULL) | ||
{ | ||
fclose(f); | ||
} | ||
} | ||
|
||
void AutosaveSave(Autosave *autosave, const char *filename) | ||
{ | ||
FILE *f = fopen(filename, "w"); | ||
char *text = NULL; | ||
json_t *root; | ||
|
||
if (f == NULL) | ||
{ | ||
printf("Error saving config '%s'\n", filename); | ||
return; | ||
} | ||
|
||
setlocale(LC_ALL, ""); | ||
|
||
root = json_new_object(); | ||
json_insert_pair_into_object(root, "Version", json_new_number("1")); | ||
AddLastMissionNode(&autosave->LastMission, root); | ||
|
||
json_tree_to_string(root, &text); | ||
fputs(json_format_string(text), f); | ||
|
||
// clean up | ||
free(text); | ||
json_free_value(&root); | ||
|
||
fclose(f);} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
C-Dogs SDL | ||
A port of the legendary (and fun) action/arcade cdogs. | ||
Copyright (c) 2013, Cong Xu | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
#ifndef _autosave | ||
#define _autosave | ||
|
||
#include <cdogs/sys_config.h> | ||
|
||
#define PASSWORD_MAX 16 | ||
#define AUTOSAVE_FILE "autosave.json" | ||
|
||
typedef struct | ||
{ | ||
char CampaignPath[CDOGS_PATH_MAX]; | ||
char Password[PASSWORD_MAX + 1]; | ||
} MissionSave; | ||
|
||
typedef struct | ||
{ | ||
MissionSave LastMission; | ||
} Autosave; | ||
|
||
void AutosaveInit(Autosave *autosave); | ||
void AutosaveLoad(Autosave *autosave, const char *filename); | ||
void AutosaveSave(Autosave *autosave, const char *filename); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <cbehave/cbehave.h> | ||
|
||
#include <autosave.h> | ||
|
||
#include <string.h> | ||
|
||
|
||
FEATURE(1, "Initialise autosave") | ||
SCENARIO("Initialise autosave") | ||
{ | ||
Autosave autosave1, autosave2; | ||
GIVEN("two autosaves") | ||
GIVEN_END | ||
|
||
WHEN("I initialise both") | ||
AutosaveInit(&autosave1); | ||
AutosaveInit(&autosave2); | ||
WHEN_END | ||
|
||
THEN("they should equal each other"); | ||
SHOULD_STR_EQUAL(autosave1.LastMission.CampaignPath, autosave2.LastMission.CampaignPath); | ||
SHOULD_STR_EQUAL(autosave1.LastMission.Password, autosave2.LastMission.Password); | ||
THEN_END | ||
} | ||
SCENARIO_END | ||
FEATURE_END | ||
|
||
FEATURE(2, "Save and load") | ||
SCENARIO("Save and load") | ||
{ | ||
Autosave autosave1, autosave2; | ||
GIVEN("an autosave with some values, and I save it to file") | ||
AutosaveInit(&autosave1); | ||
strcpy(autosave1.LastMission.CampaignPath, "path/to/file"); | ||
strcpy(autosave1.LastMission.Password, "password"); | ||
AutosaveSave(&autosave1, "tmp"); | ||
GIVEN_END | ||
|
||
WHEN("I load a second autosave from that file") | ||
AutosaveLoad(&autosave2, "tmp"); | ||
WHEN_END | ||
|
||
THEN("they should equal each other"); | ||
SHOULD_STR_EQUAL(autosave1.LastMission.CampaignPath, autosave2.LastMission.CampaignPath); | ||
SHOULD_STR_EQUAL(autosave1.LastMission.Password, autosave2.LastMission.Password); | ||
THEN_END | ||
} | ||
SCENARIO_END | ||
FEATURE_END | ||
|
||
int main(void) | ||
{ | ||
cbehave_feature features[] = | ||
{ | ||
{feature_idx(1)}, | ||
{feature_idx(2)} | ||
}; | ||
|
||
return cbehave_runner("Autosave features are:", features); | ||
} |