mirror of
https://github.com/Dark98/SliceBeam.git
synced 2026-07-02 16:49:02 +00:00
Config File Saving Updates
*Save Config On Exit *Handle New Lines A Bit Better
This commit is contained in:
@@ -879,6 +879,12 @@ public class MainActivity extends AppCompatActivity {
|
||||
delegate.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
Santoku.saveConfig();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
@@ -889,4 +895,4 @@ public class MainActivity extends AppCompatActivity {
|
||||
delegate.onDestroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,13 +75,40 @@ public class Santoku extends Application {
|
||||
}
|
||||
|
||||
public static void saveConfig() {
|
||||
if (CONFIG == null) {
|
||||
return;
|
||||
}
|
||||
Santoku.CONFIG_UID++;
|
||||
File f = getConfigFile();
|
||||
File dir = f.getParentFile();
|
||||
if (dir != null && !dir.exists()) {
|
||||
// Best effort: ensure app files dir exists before saving.
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
dir.mkdirs();
|
||||
}
|
||||
String serialized = CONFIG.serialize();
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream(f);
|
||||
fos.write(CONFIG.serialize().getBytes(StandardCharsets.UTF_8));
|
||||
File tmp = new File(f.getParentFile(), f.getName() + ".tmp");
|
||||
FileOutputStream fos = new FileOutputStream(tmp);
|
||||
fos.write(serialized.getBytes(StandardCharsets.UTF_8));
|
||||
fos.getFD().sync();
|
||||
fos.close();
|
||||
|
||||
if (f.exists() && !f.delete()) {
|
||||
Log.w("Config", "Failed to delete old config before rename: " + f.getAbsolutePath());
|
||||
}
|
||||
if (!tmp.renameTo(f)) {
|
||||
// Fallback to direct write if rename fails.
|
||||
FileOutputStream direct = new FileOutputStream(f);
|
||||
direct.write(serialized.getBytes(StandardCharsets.UTF_8));
|
||||
direct.getFD().sync();
|
||||
direct.close();
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
tmp.delete();
|
||||
}
|
||||
|
||||
// Current config should be regenerated on next slice/export.
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
getCurrentConfigFile().delete();
|
||||
} catch (Exception e) {
|
||||
Log.e("Config", "Failed to save config", e);
|
||||
|
||||
@@ -235,7 +235,7 @@ public class FileMenu extends ListBedMenu {
|
||||
Activity act = (Activity) fragment.getContext();
|
||||
Intent i = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
||||
i.setType("application/ini");
|
||||
i.putExtra(Intent.EXTRA_TITLE, "SliceBeam_config_bundle.ini");
|
||||
i.putExtra(Intent.EXTRA_TITLE, "Santoku_config_bundle.ini");
|
||||
act.startActivityForResult(i, MainActivity.REQUEST_CODE_EXPORT_PROFILES);
|
||||
}
|
||||
})
|
||||
@@ -251,7 +251,7 @@ public class FileMenu extends ListBedMenu {
|
||||
Activity act = (Activity) fragment.getContext();
|
||||
Intent i = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
||||
i.setType("application/3mf");
|
||||
i.putExtra(Intent.EXTRA_TITLE, "SliceBeam_project.3mf");
|
||||
i.putExtra(Intent.EXTRA_TITLE, "Santoku_project.3mf");
|
||||
act.startActivityForResult(i, MainActivity.REQUEST_CODE_EXPORT_3MF);
|
||||
}
|
||||
})
|
||||
|
||||
@@ -83,7 +83,12 @@ public class ConfigObject implements ProfileListFragment.ProfileListItem {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("# generated by Slice Beam ").append(BuildConfig.VERSION_NAME).append("\n\n");
|
||||
for (Map.Entry<String, String> en : values.entrySet()) {
|
||||
sb.append(en.getKey()).append(" = ").append(en.getValue().replace("\n", "\\n")).append("\n");
|
||||
String value = en.getValue();
|
||||
if (value != null) {
|
||||
value = value.replace("\r\n", "\n").replace("\r", "\n");
|
||||
value = value.replace("\n", "\\n");
|
||||
}
|
||||
sb.append(en.getKey()).append(" = ").append(value).append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@@ -200,7 +200,12 @@ public class Slic3rConfigWrapper {
|
||||
sb.append("[").append(key).append(":").append(cfg.getTitle()).append("]\n");
|
||||
|
||||
for (Map.Entry<String, String> en : cfg.values.entrySet()) {
|
||||
sb.append(en.getKey()).append(" = ").append(en.getValue().replace("\n", "\\n")).append("\n");
|
||||
String value = en.getValue();
|
||||
if (value != null) {
|
||||
value = value.replace("\r\n", "\n").replace("\r", "\n");
|
||||
value = value.replace("\n", "\\n");
|
||||
}
|
||||
sb.append(en.getKey()).append(" = ").append(value).append("\n");
|
||||
}
|
||||
sb.append("\n");
|
||||
}
|
||||
@@ -216,7 +221,12 @@ public class Slic3rConfigWrapper {
|
||||
if (presets != null) {
|
||||
sb.append("[presets]\n");
|
||||
for (Map.Entry<String, String> en : presets.values.entrySet()) {
|
||||
sb.append(en.getKey()).append(" = ").append(en.getValue().replace("\n", "\\n")).append("\n");
|
||||
String value = en.getValue();
|
||||
if (value != null) {
|
||||
value = value.replace("\r\n", "\n").replace("\r", "\n");
|
||||
value = value.replace("\n", "\\n");
|
||||
}
|
||||
sb.append(en.getKey()).append(" = ").append(value).append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user