Skip to content

Commit

Permalink
Upgrade old 2-segment themes for compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0thyj committed Jan 9, 2019
1 parent 538a588 commit c85cdfc
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 60 deletions.
128 changes: 128 additions & 0 deletions src/Compatibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;

namespace WinDynamicDesktop
{
class Compatibility
{
public static void CompatibilizeThemes()
{
if (Directory.Exists("images"))
{
UpdateThemeFileStructure();
}

UpdateThemeImageLists();
}

// TODO Remove after 2.3.0
private static void UpdateThemeFileStructure()
{
List<string> filePaths = Directory.GetFiles("themes", "*.json").ToList();
filePaths.AddRange(ThemeManager.defaultThemes.Select(
themeId => Path.Combine("themes", themeId + ".json")));

foreach (string filePath in filePaths)
{
string themeId = Path.GetFileNameWithoutExtension(filePath);
Directory.CreateDirectory(Path.Combine("themes", themeId));

if (File.Exists(filePath))
{
File.Move(filePath, Path.Combine("themes", themeId, "theme.json"));
}

ThemeConfig theme = JsonConfig.LoadTheme(themeId);
foreach (string imagePath in Directory.GetFiles("images", theme.imageFilename))
{
File.Move(imagePath,
Path.Combine("themes", themeId, Path.GetFileName(imagePath)));
}
}

if (Directory.GetFiles("images").Length == 0 &&
Directory.GetDirectories("images").Length == 0)
{
Directory.Delete("images", false);
}
}

// TODO Remove after 3.0
private static void UpdateThemeImageLists()
{
List<string> upgradeIds = new List<string>() { "BitDay", "Earth_View", "Firewatch",
"New_York", "San_Francisco", "High_Sierra"};
foreach (string filePath in Directory.EnumerateFiles("themes", "*.json",
SearchOption.AllDirectories))
{
string themeId = Path.GetFileName(Path.GetDirectoryName(filePath));

if (!upgradeIds.Contains(themeId))
{
continue;
}

string jsonText = File.ReadAllText(filePath);

if (!jsonText.Contains("sunriseImageList") ||
!jsonText.Contains("sunsetImageList"))
{
jsonText = jsonText.Replace("}",
",\"sunriseImageList\":[],\"sunsetImageList\":[]}");
ThemeConfig theme = JsonConvert.DeserializeObject<ThemeConfig>(jsonText);

if (themeId == "BitDay")
{
theme.sunriseImageList = new int[] { 12, 1 };
theme.dayImageList = new int[] { 2, 3, 4, 5, 6 };
theme.sunsetImageList = new int[] { 7, 8 };
theme.nightImageList = new int[] { 9, 10, 11 };
}
else if (themeId == "Earth_View")
{
theme.sunriseImageList = new int[] { 4, 5, 6 };
theme.dayImageList = new int[] { 7, 8, 9, 10, 11 };
theme.sunsetImageList = new int[] { 12, 13, 14 };
theme.nightImageList = new int[] { 15, 16, 1, 2, 3 };
}
else if (themeId == "Firewatch")
{
theme.sunriseImageList = new int[] { 1, 2 };
theme.dayImageList = new int[] { 3, 4, 5 };
theme.sunsetImageList = new int[] { 6, 7 };
theme.nightImageList = new int[] { 8 };
}
else if (themeId == "New_York")
{
theme.sunriseImageList = new int[] { 1, 2, 3 };
theme.dayImageList = new int[] { 4, 5, 6, 7, 8 };
theme.sunsetImageList = new int[] { 9, 10, 11, 12 };
theme.nightImageList = new int[] { 13, 14, 15, 16 };
}
else if (themeId == "San_Francisco")
{
theme.sunriseImageList = new int[] { 2, 3, 4 };
theme.dayImageList = new int[] { 5, 6, 7, 8, 9 };
theme.sunsetImageList = new int[] { 10, 16, 11, 12 };
theme.nightImageList = new int[] { 13, 14, 15, 1 };
}
else if (themeId == "High_Sierra")
{
theme.sunriseImageList = new int[] { 3, 4, 5 };
theme.dayImageList = new int[] { 6, 7, 8, 9, 10 };
theme.sunsetImageList = new int[] { 9, 1, 12, 13 };
theme.nightImageList = new int[] { 14, 15, 16, 2 };
}

string newJson = JsonConvert.SerializeObject(theme);
File.WriteAllText(filePath, newJson);
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/ThemeDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ private Image GetThumbnailImage(ThemeConfig theme, int width, int height)
return Image.FromFile(thumbnailPath);
}

int imageId1 = theme.dayImageList[(theme.dayImageList.Length + 1) / 2];
int imageId1 = theme.dayImageList[theme.dayImageList.Length / 2];
string imageFilename1 = theme.imageFilename.Replace("*", imageId1.ToString());

int imageId2 = theme.nightImageList[(theme.nightImageList.Length + 1) / 2];
int imageId2 = theme.nightImageList[theme.nightImageList.Length / 2];
string imageFilename2 = theme.imageFilename.Replace("*", imageId2.ToString());

using (var bmp1 = ShrinkImage(Path.Combine("themes", theme.themeId, imageFilename1),
Expand Down
64 changes: 14 additions & 50 deletions src/ThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,8 @@ class ThemeManager
public static void Initialize()
{
Directory.CreateDirectory("themes");
Compatibility.CompatibilizeThemes();

// TODO Remove after everyone has new file structure
if (Directory.Exists("images"))
{
UpdateThemeFileStructure();
}

List<string> themeIds = defaultThemes.ToList();

foreach (string filePath in Directory.EnumerateFiles("themes", "*.json",
Expand All @@ -45,57 +40,26 @@ public static void Initialize()

foreach (string themeId in themeIds)
{
try
{
ThemeConfig theme = JsonConfig.LoadTheme(themeId);
//try
//{
ThemeConfig theme = JsonConfig.LoadTheme(themeId);

themeSettings.Add(theme);
themeSettings.Add(theme);

if (theme.themeId == JsonConfig.settings.themeName)
{
currentTheme = theme;
}
}
catch
if (theme.themeId == JsonConfig.settings.themeName)
{
DisableTheme(themeId);
currentTheme = theme;
}
//}
//catch
//{
// DisableTheme(themeId);
//}
}

DownloadMissingImages(FindMissingThemes());
}

private static void UpdateThemeFileStructure()
{
List<string> filePaths = Directory.GetFiles("themes", "*.json").ToList();
filePaths.AddRange(defaultThemes.Select(
themeId => Path.Combine("themes", themeId + ".json")));

foreach (string filePath in filePaths)
{
string themeId = Path.GetFileNameWithoutExtension(filePath);
Directory.CreateDirectory(Path.Combine("themes", themeId));

if (File.Exists(filePath))
{
File.Move(filePath, Path.Combine("themes", themeId, "theme.json"));
}

ThemeConfig theme = JsonConfig.LoadTheme(themeId);
foreach (string imagePath in Directory.GetFiles("images", theme.imageFilename))
{
File.Move(imagePath,
Path.Combine("themes", themeId, Path.GetFileName(imagePath)));
}
}

if (Directory.GetFiles("images").Length == 0 &&
Directory.GetDirectories("images").Length == 0)
{
Directory.Delete("images", false);
}
}

public static void SelectTheme()
{
if (themeDialog == null)
Expand Down Expand Up @@ -257,8 +221,8 @@ private static void DisableTheme(string themeId)
Directory.Move(Path.Combine("themes", themeId), Path.Combine("themes", "." + themeId));

MessageBox.Show("The '" + themeId + "' theme could not be loaded and has been " +
"disabled. This is probably because it was developed for an older version of " +
"the app or its config file is formatted incorrectly.", "Error",
"disabled. This is probably because it was created for an older version of the " +
"app or its config file is formatted incorrectly.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

Expand Down
13 changes: 7 additions & 6 deletions src/WinDynamicDesktop.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="packages\Costura.Fody.3.2.2\build\Costura.Fody.props" Condition="Exists('packages\Costura.Fody.3.2.2\build\Costura.Fody.props')" />
<Import Project="packages\Costura.Fody.3.3.0\build\Costura.Fody.props" Condition="Exists('packages\Costura.Fody.3.3.0\build\Costura.Fody.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -38,17 +38,17 @@
<ApplicationIcon>WinDynamicDesktop.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="Costura, Version=3.2.2.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
<HintPath>packages\Costura.Fody.3.2.2\lib\net40\Costura.dll</HintPath>
<Reference Include="Costura, Version=3.3.0.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
<HintPath>packages\Costura.Fody.3.3.0\lib\net40\Costura.dll</HintPath>
</Reference>
<Reference Include="DesktopBridge.Helpers, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\DesktopBridge.Helpers.1.1\lib\net45\DesktopBridge.Helpers.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="RestSharp, Version=106.6.3.0, Culture=neutral, PublicKeyToken=598062e77f915f75, processorArchitecture=MSIL">
<HintPath>packages\RestSharp.106.6.3\lib\net452\RestSharp.dll</HintPath>
<Reference Include="RestSharp, Version=106.6.5.0, Culture=neutral, PublicKeyToken=598062e77f915f75, processorArchitecture=MSIL">
<HintPath>packages\RestSharp.106.6.5\lib\net452\RestSharp.dll</HintPath>
</Reference>
<Reference Include="SunCalcNet, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\SunCalcNet.1.0.2\lib\net461\SunCalcNet.dll</HintPath>
Expand Down Expand Up @@ -84,6 +84,7 @@
<DependentUpon>AboutDialog.cs</DependentUpon>
</Compile>
<Compile Include="AppContext.cs" />
<Compile Include="Compatibility.cs" />
<Compile Include="DesktopHelper.cs" />
<Compile Include="InputDialog.cs">
<SubType>Form</SubType>
Expand Down Expand Up @@ -166,7 +167,7 @@
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\Fody.3.3.5\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Fody.3.3.5\build\Fody.targets'))" />
<Error Condition="!Exists('packages\Costura.Fody.3.2.2\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.3.2.2\build\Costura.Fody.props'))" />
<Error Condition="!Exists('packages\Costura.Fody.3.3.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.3.3.0\build\Costura.Fody.props'))" />
</Target>
<Import Project="packages\Fody.3.3.5\build\Fody.targets" Condition="Exists('packages\Fody.3.3.5\build\Fody.targets')" />
</Project>
4 changes: 2 additions & 2 deletions src/packages.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Costura.Fody" version="3.2.2" targetFramework="net461" />
<package id="Costura.Fody" version="3.3.0" targetFramework="net461" />
<package id="DesktopBridge.Helpers" version="1.1" targetFramework="net461" />
<package id="Fody" version="3.3.5" targetFramework="net461" developmentDependency="true" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net461" />
<package id="RestSharp" version="106.6.3" targetFramework="net461" />
<package id="RestSharp" version="106.6.5" targetFramework="net461" />
<package id="SunCalcNet" version="1.0.2" targetFramework="net461" />
</packages>

0 comments on commit c85cdfc

Please sign in to comment.