master
ブランチ master
ShaderPlayground / src / database / setup.cc
14.3 KB、352行 生 Blame 履歴
1/*
2# 076 License
3
4Copyright (c) 076 Studio LLC
5
6Permission is hereby granted to any person obtaining a copy of the software
7Shader Playground (the "Software") to use, modify, merge, copy, publish, distribute,
8sublicense, and/or sell copies of the Software, subject to the following conditions:
9
10 1. **Origin Attribution**:
11 - You must not misrepresent the origin of the Software; you must not claim
12 you created the original Software.
13 - If the Software is used in a product, you must either:
14 a. Provide clear attribution in the product's documentation, user interface,
15 or other visible areas, **OR**
16 b. Pay the original developers a fee they specify in writing.
17 2. **Usage Restriction**:
18 - The Software, or any derivative works, dependencies, or libraries
19 incorporating it, must not be used for censorship or to suppress freedom of
20 speech, expression, or creativity. Prohibited uses include, but are not
21 limited to:
22 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
23 ideas, or objective reality.
24 - Tools or systems designed to restrict access to information or
25 artistic works.
26 3. **Notice Preservation**:
27 - This license and the above copyright notice must remain intact in all copies
28 of the source code.
29
30THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
31INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
32PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
33HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
34CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
35OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37#include <database/setup.hh>
38#include <shader/shader.hh>
39#include <util/sqlitepp.hh>
40#include <sqlite3.h>
41#include <version.hh>
42#include <cassert>
43#include <iostream>
44
45namespace db {
46 void setDefaultSetting(sqlitepp::Instance &db, const string &key, i32 value) {
47 sqlitepp::Stmt stmt(db.GetDB());
48 stmt.Prepare("INSERT INTO settings (key, value) VALUES (?, ?);");
49 stmt.BindText(1, key);
50 stmt.BindInt(2, value);
51 assert(stmt.Step() == SQLITE_DONE && "最初のシェーダーを入れ込みに失敗。\n");
52 }
53
54 void createLanguage(sqlitepp::Instance &db, const string &name, const string &code) {
55 sqlitepp::Stmt stmt(db.GetDB());
56 stmt.Prepare("INSERT INTO languages (name, code) VALUES (?, ?);");
57 stmt.BindText(1, name);
58 stmt.BindText(2, code);
59 assert(stmt.Step() == SQLITE_DONE && "最初のシェーダーを入れ込みに失敗。\n");
60 }
61
62 void createWord(sqlitepp::Instance &db, i32 langId, const string &key, const string &value = "") {
63 sqlitepp::Stmt stmt(db.GetDB());
64 stmt.Prepare("INSERT INTO locale (language_id, key, value) VALUES (?, ?, ?);");
65 stmt.BindInt(1, langId);
66 stmt.BindText(2, key);
67 stmt.BindText(3, value);
68 assert(stmt.Step() == SQLITE_DONE && "最初のシェーダーを入れ込みに失敗。\n");
69 }
70
71 void Initialize(sqlitepp::Instance &db) {
72 cstr sql = R"(CREATE TABLE IF NOT EXISTS version (
73 id INTEGER PRIMARY KEY,
74 major INTEGER NOT NULL,
75 minor INTEGER NOT NULL,
76 revision INTEGER NOT NULL,
77 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
78 updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
79);
80
81CREATE TRIGGER IF NOT EXISTS version_updated_at
82AFTER UPDATE ON version
83BEGIN
84 UPDATE version
85 SET updated_at = CURRENT_TIMESTAMP
86 WHERE id = NEW.id;
87END;
88
89CREATE TABLE IF NOT EXISTS scenes (
90 id INTEGER PRIMARY KEY,
91 name TEXT UNIQUE NOT NULL,
92 description TEXT,
93 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
94 updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
95);
96
97CREATE UNIQUE INDEX IF NOT EXISTS idx_scenes_name ON scenes(name);
98
99CREATE TRIGGER IF NOT EXISTS scenes_updated_at
100AFTER UPDATE ON scenes
101BEGIN
102 UPDATE scenes
103 SET updated_at = CURRENT_TIMESTAMP
104 WHERE id = NEW.id;
105END;
106
107CREATE TABLE IF NOT EXISTS scene_skybox (
108 id INTEGER PRIMARY KEY,
109 scene_id INTEGER NOT NULL,
110 clear_r REAL NOT NULL,
111 clear_g REAL NOT NULL,
112 clear_b REAL NOT NULL,
113 clear_a REAL NOT NULL,
114 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
115 updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
116
117 FOREIGN KEY (scene_id) REFERENCES scenes(id) ON DELETE CASCADE
118);
119
120CREATE TRIGGER IF NOT EXISTS scene_skybox_updated_at
121AFTER UPDATE ON scene_skybox
122BEGIN
123 UPDATE scene_skybox
124 SET updated_at = CURRENT_TIMESTAMP
125 WHERE id = NEW.id;
126END;
127
128CREATE TABLE IF NOT EXISTS scene_code (
129 id INTEGER PRIMARY KEY,
130 scene_id INTEGER NOT NULL,
131 code_type INTEGER NOT NULL,
132 code TEXT NOT NULL,
133 filename TEXT,
134 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
135 updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
136
137 FOREIGN KEY (scene_id) REFERENCES scenes(id) ON DELETE CASCADE
138);
139
140CREATE TRIGGER IF NOT EXISTS scene_code_updated_at
141AFTER UPDATE ON scene_code
142BEGIN
143 UPDATE scene_code
144 SET updated_at = CURRENT_TIMESTAMP
145 WHERE id = NEW.id;
146END;
147
148CREATE TABLE IF NOT EXISTS languages (
149 id INTEGER PRIMARY KEY,
150 name TEXT NOT NULL,
151 code TEXT NOT NULL
152);
153
154CREATE TABLE IF NOT EXISTS locale (
155 id INTEGER PRIMARY KEY,
156 language_id INTEGER,
157 key TEXT NOT NULL,
158 value TEXT,
159
160 FOREIGN KEY (language_id) REFERENCES languages(id) ON DELETE CASCADE
161);
162
163CREATE TABLE IF NOT EXISTS settings (
164 id INTEGER PRIMARY KEY,
165 key TEXT NOT NULL,
166 value INTEGER
167);)";
168
169 db.Exec(sql);
170
171 {
172 sqlitepp::Stmt stmt(db.GetDB());
173 stmt.Prepare("SELECT COUNT(*) FROM version;");
174 if (stmt.Step() == SQLITE_ROW) {
175 int count = stmt.ColumnInt(0);
176 if (count > 0) return;
177 }
178 }
179
180 {
181 sqlitepp::Stmt stmt(db.GetDB());
182 stmt.Prepare("INSERT INTO version (major, minor, revision) VALUES (?, ?, ?);");
183 stmt.BindInt(1, version::major);
184 stmt.BindInt(2, version::minor);
185 stmt.BindInt(3, version::revision);
186
187 assert(stmt.Step() == SQLITE_DONE && "最初のシェーダーを入れ込みに失敗。\n");
188 }
189
190 {
191 setDefaultSetting(db, "locale", 1);
192 }
193
194 {
195 createLanguage(db, "日本語", "ja_JP");
196 createLanguage(db, "English", "en_US");
197 }
198
199 {
200 createWord(db, 1, "file", "ファイル(F)");
201 createWord(db, 2, "file", "File");
202 createWord(db, 1, "filenew", "新しいファイル(N)...");
203 createWord(db, 2, "filenew", "New File...");
204 createWord(db, 1, "filenewcreateshader", "新しいシェーダーを作成");
205 createWord(db, 2, "filenewcreateshader", "New Shader Creation");
206 createWord(db, 1, "filenew2dshader", "2D シェーダー");
207 createWord(db, 2, "filenew2dshader", "2D Shader");
208 createWord(db, 1, "filenew3dshader", "3D シェーダー");
209 createWord(db, 2, "filenew3dshader", "3D Shader");
210 createWord(db, 1, "filenewsceneshader", "シーン シェーダー");
211 createWord(db, 2, "filenewsceneshader", "Scene Shader");
212 createWord(db, 1, "fileopen", "開く(O)...");
213 createWord(db, 2, "fileopen", "Open...");
214 createWord(db, 1, "fileopenshader", "シェーダーの開く");
215 createWord(db, 2, "fileopenshader", "Open Shader");
216 createWord(db, 1, "filesave", "保存(S)");
217 createWord(db, 2, "filesave", "Save");
218 createWord(db, 1, "filesaveas", "名前を付けて保存(A)...");
219 createWord(db, 2, "filesaveas", "Save as...");
220 createWord(db, 1, "filesaveasshader", "シェーダーの保存");
221 createWord(db, 2, "filesaveasshader", "Save Shader");
222 createWord(db, 1, "fileshadername", "シェーダー名");
223 createWord(db, 2, "fileshadername", "Shader Name");
224 createWord(db, 1, "quit", "終了(X)");
225 createWord(db, 2, "quit", "Quit");
226 createWord(db, 1, "cancel", "キャンセル(C)");
227 createWord(db, 2, "cancel", "Cancel");
228 createWord(db, 1, "ok", "OK(O)");
229 createWord(db, 2, "ok", "OK");
230 createWord(db, 1, "editorvertshader", "頂点シェーダー");
231 createWord(db, 2, "editorvertshader", "Vertex Shader");
232 createWord(db, 1, "editorfragshader", "フラグメントシェーダー");
233 createWord(db, 2, "editorfragshader", "Fragment Shader");
234 createWord(db, 1, "editorlua", "Luaエディター");
235 createWord(db, 2, "editorlua", "Lua Editor");
236 createWord(db, 1, "editorviewport", "ビューポート");
237 createWord(db, 2, "editorviewport", "Viewport");
238 createWord(db, 1, "editorconsole", "コンソール");
239 createWord(db, 2, "editorconsole", "Console");
240 createWord(db, 1, "editorconsoleclear", "消す(X)");
241 createWord(db, 2, "editorconsoleclear", "Clear");
242 createWord(db, 1, "edit", "編集(E)");
243 createWord(db, 2, "edit", "Edit");
244 createWord(db, 1, "editundo", "元に戻す(U)");
245 createWord(db, 2, "editundo", "Undo");
246 createWord(db, 1, "editredo", "やり直し(R)");
247 createWord(db, 2, "editredo", "Redo");
248 createWord(db, 1, "editcut", "切り取り(C)");
249 createWord(db, 2, "editcut", "Cut");
250 createWord(db, 1, "editcopy", "コピー(Y)");
251 createWord(db, 2, "editcopy", "Copy");
252 createWord(db, 1, "editpaste", "貼り付け(P)");
253 createWord(db, 2, "editpaste", "Paste");
254 createWord(db, 1, "editselectall", "全て洗濯(A)");
255 createWord(db, 2, "editselectall", "Select All");
256 createWord(db, 1, "editduplicate", "行を複製(D)");
257 createWord(db, 2, "editduplicate", "Duplicate");
258 createWord(db, 1, "settings", "設定(S)");
259 createWord(db, 2, "settings", "Settings");
260 createWord(db, 1, "build", "ビルド(B)");
261 createWord(db, 2, "build", "Build");
262 createWord(db, 1, "buildcompile", "コンパイル(C)");
263 createWord(db, 2, "buildcompile", "Compile");
264 createWord(db, 1, "help", "ヘルプ(H)");
265 createWord(db, 2, "help", "Help");
266 createWord(db, 1, "helpmanual", "マニュアル(M)");
267 createWord(db, 2, "helpmanual", "Manual");
268 createWord(db, 1, "helpmanualoverview", "概要");
269 createWord(db, 2, "helpmanualoverview", "Overview");
270 createWord(db, 1, "helpmanualshader", "シェーダー");
271 createWord(db, 2, "helpmanualshader", "Shader");
272 createWord(db, 1, "helpmanuallua", "Luaエンジン");
273 createWord(db, 2, "helpmanuallua", "Lua Engine");
274 createWord(db, 1, "helpmanualhotkey", "ホットキー");
275 createWord(db, 2, "helpmanualhotkey", "Hotkeys");
276 createWord(db, 1, "helpabout", "Shader Playgroundについて(A)");
277 createWord(db, 2, "helpabout", "About Shader Playground");
278 createWord(db, 1, "currentfps", "現在フレームレート");
279 createWord(db, 2, "currentfps", "Current Frame Rate");
280 createWord(db, 1, "consolelogerrorluainvalidcomp", "Luaコードが不正ですので、コンパイル出来ませんでした。");
281 createWord(db, 2, "consolelogerrorluainvalidcomp", "Your Lua code is invalid, so it could not be compiled.");
282 createWord(db, 1, "consolelogerrorluainvalidsave", "Luaコードが不正ですので、保存出来ませんでした。");
283 createWord(db, 2, "consolelogerrorluainvalidsave", "Your Lua code is invalid, so it could not be saved.");
284 createWord(db, 1, "consoleloginfocompileok", "シェーダーをコンパイルしました。");
285 createWord(db, 2, "consoleloginfocompileok", "Shaders got compiled successfully.");
286 createWord(db, 1, "consoleloginfosaveok", "シェーダー及びLuaを保存しました。");
287 createWord(db, 2, "consoleloginfosaveok", "Shaders and Lua have been saved.");
288 createWord(db, 1, "consoleloginfolangchangeok", "言語を変更しました。");
289 createWord(db, 2, "consoleloginfolangchangeok", "Language has been changed.");
290 createWord(db, 1, "consoleloginfocreateok", "新しいシェーダーを作成しました。保存するには、「CTRL + S」を押して下さい。");
291 createWord(db, 2, "consoleloginfocreateok", "New shader has been created. In order to save, press \"CTRL + S\".");
292 createWord(db, 1, "clearcolor", "クリアカラー");
293 createWord(db, 2, "clearcolor", "Clear color");
294 }
295
296 i64 shaderId = 0;
297
298 {
299 sqlitepp::Stmt stmt(db.GetDB());
300 stmt.Prepare("INSERT INTO scenes (name, description) VALUES (?, ?);");
301 stmt.BindText(1, "default");
302 stmt.BindText(2, "");
303 assert(stmt.Step() == SQLITE_DONE && "最初のシェーダーを入れ込みに失敗。\n");
304 shaderId = db.GetLastInsertId();
305 }
306
307 {
308 sqlitepp::Stmt stmt(db.GetDB());
309 stmt.Prepare("INSERT INTO scene_skybox (scene_id, clear_r, clear_g, clear_b, clear_a) VALUES (?, ?, ?, ?, ?);");
310 stmt.BindInt(1, shaderId);
311 stmt.BindDouble(2, 0.f);
312 stmt.BindDouble(3, 0.f);
313 stmt.BindDouble(4, 0.f);
314 stmt.BindDouble(5, 1.f);
315
316 assert(stmt.Step() == SQLITE_DONE && "最初のシェーダーを入れ込みに失敗。\n");
317 }
318
319 {
320 sqlitepp::Stmt stmt(db.GetDB());
321 stmt.Prepare("INSERT INTO scene_code (scene_id, code_type, code, filename) VALUES (?, ?, ?, ?);");
322 stmt.BindInt(1, shaderId);
323 stmt.BindInt(2, static_cast<int>(db::ShaderCodeType::GlslVertex));
324 stmt.BindText(3, GetDefaultVertexShader());
325 stmt.BindText(4, "default.vert");
326
327 assert(stmt.Step() == SQLITE_DONE && "最初のシェーダーを入れ込みに失敗。\n");
328 }
329
330 {
331 sqlitepp::Stmt stmt(db.GetDB());
332 stmt.Prepare("INSERT INTO scene_code (scene_id, code_type, code, filename) VALUES (?, ?, ?, ?);");
333 stmt.BindInt(1, shaderId);
334 stmt.BindInt(2, static_cast<int>(db::ShaderCodeType::GlslFragment));
335 stmt.BindText(3, GetDefaultFragmentShader());
336 stmt.BindText(4, "default.frag");
337
338 assert(stmt.Step() == SQLITE_DONE && "最初のシェーダーを入れ込みに失敗。\n");
339 }
340
341 {
342 sqlitepp::Stmt stmt(db.GetDB());
343 stmt.Prepare("INSERT INTO scene_code (scene_id, code_type, code, filename) VALUES (?, ?, ?, ?);");
344 stmt.BindInt(1, shaderId);
345 stmt.BindInt(2, static_cast<int>(db::ShaderCodeType::Lua));
346 stmt.BindText(3, GetDefaultLuaCode());
347 stmt.BindText(4, "default.lua");
348
349 assert(stmt.Step() == SQLITE_DONE && "最初のシェーダーを入れ込みに失敗。\n");
350 }
351 }
352} // namespace db