mirror of
https://github.com/Dark98/SliceBeam.git
synced 2026-07-06 08:39:05 +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();
|
delegate.onPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStop() {
|
||||||
|
super.onStop();
|
||||||
|
Santoku.saveConfig();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
|||||||
@@ -75,13 +75,40 @@ public class Santoku extends Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void saveConfig() {
|
public static void saveConfig() {
|
||||||
|
if (CONFIG == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
Santoku.CONFIG_UID++;
|
Santoku.CONFIG_UID++;
|
||||||
File f = getConfigFile();
|
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 {
|
try {
|
||||||
FileOutputStream fos = new FileOutputStream(f);
|
File tmp = new File(f.getParentFile(), f.getName() + ".tmp");
|
||||||
fos.write(CONFIG.serialize().getBytes(StandardCharsets.UTF_8));
|
FileOutputStream fos = new FileOutputStream(tmp);
|
||||||
|
fos.write(serialized.getBytes(StandardCharsets.UTF_8));
|
||||||
|
fos.getFD().sync();
|
||||||
fos.close();
|
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();
|
getCurrentConfigFile().delete();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e("Config", "Failed to save config", e);
|
Log.e("Config", "Failed to save config", e);
|
||||||
|
|||||||
@@ -235,7 +235,7 @@ public class FileMenu extends ListBedMenu {
|
|||||||
Activity act = (Activity) fragment.getContext();
|
Activity act = (Activity) fragment.getContext();
|
||||||
Intent i = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
Intent i = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
||||||
i.setType("application/ini");
|
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);
|
act.startActivityForResult(i, MainActivity.REQUEST_CODE_EXPORT_PROFILES);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -251,7 +251,7 @@ public class FileMenu extends ListBedMenu {
|
|||||||
Activity act = (Activity) fragment.getContext();
|
Activity act = (Activity) fragment.getContext();
|
||||||
Intent i = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
Intent i = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
||||||
i.setType("application/3mf");
|
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);
|
act.startActivityForResult(i, MainActivity.REQUEST_CODE_EXPORT_3MF);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -83,7 +83,12 @@ public class ConfigObject implements ProfileListFragment.ProfileListItem {
|
|||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("# generated by Slice Beam ").append(BuildConfig.VERSION_NAME).append("\n\n");
|
sb.append("# generated by Slice Beam ").append(BuildConfig.VERSION_NAME).append("\n\n");
|
||||||
for (Map.Entry<String, String> en : values.entrySet()) {
|
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();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -200,7 +200,12 @@ public class Slic3rConfigWrapper {
|
|||||||
sb.append("[").append(key).append(":").append(cfg.getTitle()).append("]\n");
|
sb.append("[").append(key).append(":").append(cfg.getTitle()).append("]\n");
|
||||||
|
|
||||||
for (Map.Entry<String, String> en : cfg.values.entrySet()) {
|
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");
|
sb.append("\n");
|
||||||
}
|
}
|
||||||
@@ -216,7 +221,12 @@ public class Slic3rConfigWrapper {
|
|||||||
if (presets != null) {
|
if (presets != null) {
|
||||||
sb.append("[presets]\n");
|
sb.append("[presets]\n");
|
||||||
for (Map.Entry<String, String> en : presets.values.entrySet()) {
|
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