2006-03-20 16:43:15 +00:00
|
|
|
/*
|
2014-06-01 17:55:23 +00:00
|
|
|
* Portions of this file are copyright Rebirth contributors and licensed as
|
|
|
|
* described in COPYING.txt.
|
|
|
|
* Portions of this file are copyright Parallax Software and licensed
|
|
|
|
* according to the Parallax license below.
|
|
|
|
* See COPYING.txt for license details.
|
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
|
|
|
|
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
|
|
|
|
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
|
|
|
|
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
|
|
|
|
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
|
|
|
|
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
|
|
|
|
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
|
|
|
|
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
|
2010-12-24 04:26:24 +00:00
|
|
|
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
|
2006-03-20 16:43:15 +00:00
|
|
|
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
|
|
|
|
*/
|
2008-04-06 20:23:28 +00:00
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
/*
|
|
|
|
*
|
2008-01-23 17:25:09 +00:00
|
|
|
* Autosave system:
|
|
|
|
* Saves current mine to disk to prevent loss of work, and support undo.
|
2006-03-20 16:43:15 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2012-07-07 18:35:06 +00:00
|
|
|
#include "dxxerror.h"
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
#include "inferno.h"
|
|
|
|
#include "editor.h"
|
|
|
|
#include "u_mem.h"
|
|
|
|
#include "ui.h"
|
2013-03-16 03:10:55 +00:00
|
|
|
#include "strutil.h"
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2018-08-26 18:10:36 +00:00
|
|
|
#include "compiler-cf_assert.h"
|
|
|
|
|
2015-12-13 18:00:49 +00:00
|
|
|
namespace dcx {
|
2015-12-05 22:57:24 +00:00
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
#define AUTOSAVE_PERIOD 5 // Number of minutes for timed autosave
|
|
|
|
|
|
|
|
int Autosave_count;
|
2020-10-12 03:28:25 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-12-04 03:36:31 +00:00
|
|
|
static int Autosave_numfiles;
|
|
|
|
static int Autosave_total;
|
|
|
|
static int undo_count;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-10-12 03:28:25 +00:00
|
|
|
const char *set_autosave_name(mine_filename_type &out, const std::array<char, PATH_MAX> &in, const unsigned i)
|
|
|
|
{
|
|
|
|
d_strupr(out, in);
|
|
|
|
char *ext;
|
|
|
|
#define DXX_AUTOSAVE_MINE_EXTENSION ".MIN"
|
|
|
|
if (!strcmp(out.data(), "*" DXX_AUTOSAVE_MINE_EXTENSION))
|
|
|
|
{
|
|
|
|
#define DXX_DEFAULT_AUTOSAVE_MINE_NAME "TEMP" DXX_AUTOSAVE_MINE_EXTENSION
|
|
|
|
strcpy(out.data(), DXX_DEFAULT_AUTOSAVE_MINE_NAME);
|
|
|
|
ext = &out[sizeof(DXX_DEFAULT_AUTOSAVE_MINE_NAME) - sizeof(DXX_AUTOSAVE_MINE_EXTENSION)];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ext = strstr(out.data(), DXX_AUTOSAVE_MINE_EXTENSION);
|
|
|
|
if (!ext)
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
#undef DXX_AUTOSAVE_MINE_EXTENSION
|
|
|
|
cf_assert(i < 10);
|
|
|
|
snprintf(ext + 2, 3, "%u", i);
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
2015-12-04 03:36:31 +00:00
|
|
|
static int Timer_save_flag;
|
2020-10-12 03:28:25 +00:00
|
|
|
|
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
int Autosave_flag;
|
|
|
|
|
2020-05-02 21:18:42 +00:00
|
|
|
std::array<const char *, 10> undo_status;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
void init_autosave(void) {
|
|
|
|
// int i;
|
|
|
|
|
|
|
|
Autosave_count = 0;
|
|
|
|
Autosave_numfiles = 0;
|
|
|
|
Autosave_flag = 0;
|
|
|
|
undo_count = 0;
|
|
|
|
autosave_mine(mine_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_autosave(void) {
|
2018-08-26 18:10:36 +00:00
|
|
|
const unsigned t = Autosave_total;
|
|
|
|
cf_assert(t < 10);
|
|
|
|
for (unsigned i = 0; i < t; ++i)
|
|
|
|
{
|
2020-10-12 03:28:25 +00:00
|
|
|
mine_filename_type delname;
|
|
|
|
if (set_autosave_name(delname, mine_filename, i))
|
|
|
|
PHYSFS_delete(delname.data());
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 03:28:25 +00:00
|
|
|
void autosave_mine(const std::array<char, PATH_MAX> &name)
|
2020-09-28 03:32:15 +00:00
|
|
|
{
|
2006-03-20 16:43:15 +00:00
|
|
|
if (Autosave_flag) {
|
2020-10-12 03:28:25 +00:00
|
|
|
mine_filename_type savename;
|
|
|
|
if (!set_autosave_name(savename, name, Autosave_count))
|
|
|
|
return;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
med_save_mine( savename );
|
|
|
|
Autosave_count++;
|
|
|
|
if (undo_count > 0) undo_count--;
|
|
|
|
if (Autosave_count > 9) Autosave_count -= 10;
|
|
|
|
if (Autosave_numfiles < 10)
|
|
|
|
Autosave_numfiles++;
|
|
|
|
if (Autosave_total < 10)
|
|
|
|
Autosave_total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 02:36:56 +00:00
|
|
|
tm Editor_time_of_day;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-12-27 22:03:09 +00:00
|
|
|
static void print_clock(grs_canvas &canvas, const grs_font &cv_font)
|
2015-04-02 02:36:56 +00:00
|
|
|
{
|
2017-02-11 21:42:34 +00:00
|
|
|
gr_set_fontcolor(canvas, CBLACK, CGREY);
|
2020-05-02 21:18:42 +00:00
|
|
|
std::array<char, 20> message;
|
2015-04-02 02:36:56 +00:00
|
|
|
if (!strftime(message.data(), message.size(), "%m-%d %H:%M:%S", &Editor_time_of_day))
|
|
|
|
message[0] = 0;
|
2021-09-12 16:20:52 +00:00
|
|
|
const auto &&[w, h] = gr_get_string_size(cv_font, message.data());
|
2020-07-16 02:31:04 +00:00
|
|
|
const auto color = CGREY;
|
2017-02-11 21:42:34 +00:00
|
|
|
gr_rect(canvas, 700, 0, 799, h + 1, color);
|
2020-12-27 22:03:09 +00:00
|
|
|
gr_string(canvas, cv_font, 700, 0, message.data(), w, h);
|
2017-02-11 21:42:34 +00:00
|
|
|
gr_set_fontcolor(canvas, CBLACK, CWHITE);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_editor_time_of_day()
|
|
|
|
{
|
|
|
|
time_t ltime;
|
|
|
|
|
|
|
|
time( <ime );
|
|
|
|
Editor_time_of_day = *localtime( <ime );
|
|
|
|
}
|
|
|
|
|
2020-10-12 03:28:25 +00:00
|
|
|
void TimedAutosave(const std::array<char, PATH_MAX> &name)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-12-27 22:03:09 +00:00
|
|
|
auto &canvas = grd_curscreen->sc_canvas;
|
|
|
|
gr_set_current_canvas(canvas);
|
2012-04-09 02:12:58 +00:00
|
|
|
{
|
2020-12-27 22:03:09 +00:00
|
|
|
print_clock(canvas, *canvas.cv_font);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef DEMO
|
2015-04-02 02:36:56 +00:00
|
|
|
const auto &minute = Editor_time_of_day.tm_min;
|
2006-03-20 16:43:15 +00:00
|
|
|
if (minute%AUTOSAVE_PERIOD != 0)
|
|
|
|
Timer_save_flag = 1;
|
|
|
|
|
|
|
|
if ((minute%AUTOSAVE_PERIOD == 0) && (Timer_save_flag)) {
|
|
|
|
time_t ltime;
|
|
|
|
|
|
|
|
autosave_mine(name);
|
|
|
|
Timer_save_flag = 0;
|
|
|
|
time( <ime );
|
2013-06-23 22:21:19 +00:00
|
|
|
diagnostic_message_fmt("Mine Autosaved at %s\n", ctime(<ime));
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int undo( void ) {
|
|
|
|
Int3();
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:57:24 +00:00
|
|
|
|
|
|
|
}
|