Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | /* |
2 | * Portions of this file are copyright Rebirth contributors and licensed as |
||
3 | * described in COPYING.txt. |
||
4 | * Portions of this file are copyright Parallax Software and licensed |
||
5 | * according to the Parallax license below. |
||
6 | * See COPYING.txt for license details. |
||
7 | |||
8 | THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
||
9 | SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
||
10 | END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
||
11 | ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
||
12 | IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
||
13 | SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
||
14 | FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
||
15 | CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
||
16 | AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
||
17 | COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
||
18 | */ |
||
19 | |||
20 | /* |
||
21 | * |
||
22 | * Autosave system: |
||
23 | * Saves current mine to disk to prevent loss of work, and support undo. |
||
24 | * |
||
25 | */ |
||
26 | |||
27 | #include <stdio.h> |
||
28 | #include <stdlib.h> |
||
29 | #include <stdarg.h> |
||
30 | #include <math.h> |
||
31 | #include <string.h> |
||
32 | #include <time.h> |
||
33 | |||
34 | #include "dxxerror.h" |
||
35 | |||
36 | #include "inferno.h" |
||
37 | #include "editor.h" |
||
38 | #include "u_mem.h" |
||
39 | #include "ui.h" |
||
40 | #include "strutil.h" |
||
41 | |||
42 | #include "compiler-cf_assert.h" |
||
43 | |||
44 | namespace dcx { |
||
45 | |||
46 | #define AUTOSAVE_PERIOD 5 // Number of minutes for timed autosave |
||
47 | |||
48 | int Autosave_count; |
||
49 | static int Autosave_numfiles; |
||
50 | static int Autosave_total; |
||
51 | static int undo_count; |
||
52 | |||
53 | static int Timer_save_flag; |
||
54 | int Autosave_flag; |
||
55 | |||
56 | std::array<const char *, 10> undo_status; |
||
57 | |||
58 | void init_autosave(void) { |
||
59 | // int i; |
||
60 | |||
61 | Autosave_count = 0; |
||
62 | Autosave_numfiles = 0; |
||
63 | Autosave_flag = 0; |
||
64 | undo_count = 0; |
||
65 | autosave_mine(mine_filename); |
||
66 | } |
||
67 | |||
68 | void close_autosave(void) { |
||
69 | char *ext; |
||
70 | |||
71 | const unsigned t = Autosave_total; |
||
72 | cf_assert(t < 10); |
||
73 | for (unsigned i = 0; i < t; ++i) |
||
74 | { |
||
75 | |||
76 | char delname[PATH_MAX]; |
||
77 | |||
78 | strcpy ( delname, mine_filename ); |
||
79 | d_strupr( delname ); |
||
80 | if ( !strcmp(delname, "*.MIN") ) strcpy(delname, "TEMP.MIN"); |
||
81 | |||
82 | ext = strstr(delname, ".MIN"); |
||
83 | snprintf(ext + 2, 3, "%d", i); |
||
84 | |||
85 | remove( delname ); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | void autosave_mine(const char *name) { |
||
90 | char *ext; |
||
91 | |||
92 | if (Autosave_flag) { |
||
93 | |||
94 | char savename[PATH_MAX]; |
||
95 | |||
96 | |||
97 | strcpy ( savename, name ); |
||
98 | d_strupr( savename ); |
||
99 | if ( !strcmp(savename, "*.MIN") ) strcpy(savename, "TEMP.MIN"); |
||
100 | |||
101 | ext = strstr(savename, ".MIN"); |
||
102 | snprintf(ext + 2, 3, "%d", Autosave_count); |
||
103 | |||
104 | med_save_mine( savename ); |
||
105 | Autosave_count++; |
||
106 | if (undo_count > 0) undo_count--; |
||
107 | if (Autosave_count > 9) Autosave_count -= 10; |
||
108 | if (Autosave_numfiles < 10) |
||
109 | Autosave_numfiles++; |
||
110 | if (Autosave_total < 10) |
||
111 | Autosave_total++; |
||
112 | |||
113 | } |
||
114 | |||
115 | } |
||
116 | |||
117 | tm Editor_time_of_day; |
||
118 | |||
119 | static void print_clock() |
||
120 | { |
||
121 | int w, h; |
||
122 | gr_set_default_canvas(); |
||
123 | auto &canvas = *grd_curcanv; |
||
124 | gr_set_fontcolor(canvas, CBLACK, CGREY); |
||
125 | std::array<char, 20> message; |
||
126 | if (!strftime(message.data(), message.size(), "%m-%d %H:%M:%S", &Editor_time_of_day)) |
||
127 | message[0] = 0; |
||
128 | gr_get_string_size(*canvas.cv_font, message.data(), &w, &h, nullptr); |
||
129 | const auto color = CGREY; |
||
130 | gr_rect(canvas, 700, 0, 799, h + 1, color); |
||
131 | gr_string(canvas, *canvas.cv_font, 700, 0, message.data(), w, h); |
||
132 | gr_set_fontcolor(canvas, CBLACK, CWHITE); |
||
133 | } |
||
134 | |||
135 | void set_editor_time_of_day() |
||
136 | { |
||
137 | time_t ltime; |
||
138 | |||
139 | time( <ime ); |
||
140 | Editor_time_of_day = *localtime( <ime ); |
||
141 | } |
||
142 | |||
143 | void TimedAutosave(const char *name) |
||
144 | { |
||
145 | { |
||
146 | print_clock(); |
||
147 | } |
||
148 | |||
149 | |||
150 | #ifndef DEMO |
||
151 | const auto &minute = Editor_time_of_day.tm_min; |
||
152 | if (minute%AUTOSAVE_PERIOD != 0) |
||
153 | Timer_save_flag = 1; |
||
154 | |||
155 | if ((minute%AUTOSAVE_PERIOD == 0) && (Timer_save_flag)) { |
||
156 | time_t ltime; |
||
157 | |||
158 | autosave_mine(name); |
||
159 | Timer_save_flag = 0; |
||
160 | time( <ime ); |
||
161 | diagnostic_message_fmt("Mine Autosaved at %s\n", ctime(<ime)); |
||
162 | } |
||
163 | #endif |
||
164 | |||
165 | } |
||
166 | |||
167 | |||
168 | int undo( void ) { |
||
169 | Int3(); |
||
170 | return 2; |
||
171 | } |
||
172 | |||
173 | |||
174 | } |