Skip to content

Commit

Permalink
Embedded LibVLC
Browse files Browse the repository at this point in the history
Fixed Some Errors
Borderless Window Dragging
Better Settings
Force Audio Channel Mode
Fixed Channel down from index 0
  • Loading branch information
FoxCouncil committed Feb 15, 2019
1 parent 95b0141 commit a0b081b
Show file tree
Hide file tree
Showing 351 changed files with 665 additions and 123 deletions.
18 changes: 18 additions & 0 deletions FoxIPTV/Classes/Difference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2019 Fox Council - MIT License - https://github.com/FoxCouncil/FoxIPTV

namespace FoxIPTV.Classes
{
public class Difference
{
public string PropertyName { get; set; }

public object ValueA { get; set; }

public object ValueB { get; set; }

public override string ToString()
{
return $"{PropertyName} Changed {ValueA ?? "NULL"} -> {ValueB ?? "NULL"}";
}
}
}
53 changes: 46 additions & 7 deletions FoxIPTV/Classes/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
// Copyright (c) 2019 Fox Council - MIT License - https://github.com/FoxCouncil/FoxIPTV

using System;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace FoxIPTV.Classes
{
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

// ReSharper disable once UnusedMember.Global
public static class Extensions
{
public static List<Difference> Difference<T>(this T valueA, T valueB)
{
var differences = new List<Difference>();

var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

foreach (var property in properties)
{
var v = new Difference
{
PropertyName = property.Name,
ValueA = property.GetValue(valueA),
ValueB = property.GetValue(valueB)
};

if (v.ValueA == null && v.ValueB == null)
{
continue;
}

if (v.ValueA == null && v.ValueB != null || v.ValueA != null && v.ValueB == null)
{
differences.Add(v);

continue;
}

if (v.ValueA != null && !v.ValueA.Equals(v.ValueB))
{
differences.Add(v);
}
}

return differences;
}

public static DateTime RoundUp(this DateTime dt, TimeSpan d)
{
return new DateTime((dt.Ticks + d.Ticks - 1) / d.Ticks * d.Ticks, dt.Kind);
Expand Down Expand Up @@ -76,7 +115,7 @@ public static string Protect(this string clearText, string optionalEntropy = nul
{
if (clearText == null)
{
throw new ArgumentNullException("clearText");
throw new ArgumentNullException(nameof(clearText));
}

var clearBytes = Encoding.UTF8.GetBytes(clearText);
Expand All @@ -90,7 +129,7 @@ public static string Unprotect(this string encryptedText, string optionalEntropy
{
if (encryptedText == null)
{
throw new ArgumentNullException("encryptedText");
throw new ArgumentNullException(nameof(encryptedText));
}

var encryptedBytes = Convert.FromBase64String(encryptedText);
Expand Down
39 changes: 25 additions & 14 deletions FoxIPTV/Classes/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace FoxIPTV.Classes
{
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using Newtonsoft.Json;

Expand Down Expand Up @@ -46,18 +47,33 @@ public class Settings
private readonly ReaderWriterLockSlim _fileLock = new ReaderWriterLockSlim();

private readonly string _filePath = Path.Combine(TvCore.UserStoragePath, "udata");
private Settings _loadedSettingsData;

public void Save()
{
TvCore.LogDebug($"[Settings] Writing settings file {_filePath}");
var differences = _loadedSettingsData.Difference(this);

if (differences.Count == 0)
{
return;
}

_fileLock.EnterWriteLock();

#if DEBUG
foreach (var diff in differences)
{
TvCore.LogDebug($"[Settings] {diff}");
}
#endif

try
{
var settingsData = JsonConvert.SerializeObject(this);

File.WriteAllText(_filePath, settingsData);

_loadedSettingsData = JsonConvert.DeserializeObject<Settings>(settingsData);
}
finally
{
Expand All @@ -80,25 +96,20 @@ public void Load()

var fileContents = File.ReadAllText(_filePath);

var settingsData = JsonConvert.DeserializeObject<Settings>(fileContents);
_loadedSettingsData = JsonConvert.DeserializeObject<Settings>(fileContents);

var settingsType = _loadedSettingsData.GetType();

MapProperties(settingsData);
foreach (var setting in settingsType.GetProperties())
{
var currentProperty = GetType().GetProperty(setting.Name);
currentProperty?.SetValue(this, setting.GetValue(_loadedSettingsData));
}
}
finally
{
_fileLock.ExitReadLock();
}
}

private void MapProperties(Settings newSettingsData)
{
var settingsType = newSettingsData.GetType();

foreach (var setting in settingsType.GetProperties())
{
var currentProperty = GetType().GetProperty(setting.Name);
currentProperty?.SetValue(this, setting.GetValue(newSettingsData));
}
}
}
}
105 changes: 90 additions & 15 deletions FoxIPTV/Classes/TvCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ namespace FoxIPTV.Classes
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using Properties;
using Timer = System.Timers.Timer;

public static class TvCore
Expand All @@ -30,6 +32,8 @@ public static class TvCore

private static readonly ConcurrentDictionary<uint, byte[]> _imageCache = new ConcurrentDictionary<uint, byte[]>();

private static readonly object _logWriterLock = new object();

private static readonly StreamWriter _logWriter;

private static readonly FixedQueue<string> _logBuffer = new FixedQueue<string> { FixedSize = 1000 };
Expand Down Expand Up @@ -104,8 +108,13 @@ static TvCore()

LogInfo("[TVCore] Startup: Binding ThreadException & UnhandledException...");

Application.ThreadException += (s, a) => HandleAppException(a.Exception);
AppDomain.CurrentDomain.UnhandledException += (s, a) => HandleAppException(a.ExceptionObject as Exception);
void LogException(Exception ex)
{
LogError($"[Exception] Unhandled: {ex.Message}\n{ex.StackTrace}");
}

Application.ThreadException += (s, a) => LogException(a.Exception);
AppDomain.CurrentDomain.UnhandledException += (s, a) => LogException(a.ExceptionObject as Exception);

try
{
Expand All @@ -127,6 +136,59 @@ static TvCore()

LogDebug("[TVCore] Startup: Application directories created");

const string namespaceValue = "FoxIPTV.LibVLC.";

var assembly = Assembly.GetExecutingAssembly();
var resources = assembly.GetManifestResourceNames().Where(x => x.StartsWith(namespaceValue));

foreach (var rsc in resources)
{
var libName = rsc.Replace(namespaceValue, string.Empty);
var libNameSub = libName.Substring(0, libName.IndexOf(".dll", StringComparison.Ordinal));
var libNamePath = libNameSub.Replace('.', '\\');

libName = libName.Replace(libNameSub, libNamePath);

var pathSepIdx = libNamePath.LastIndexOf('\\');

string libFilename;
string dir;

if (pathSepIdx != -1)
{
libNamePath = libNamePath.Substring(0, pathSepIdx);

dir = Path.Combine(LibraryPath, libNamePath);

if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

libFilename = libName.Substring(pathSepIdx + 1);
}
else
{
dir = LibraryPath;
libFilename = libName;
}

var libFullPath = Path.Combine(dir, libFilename);

if (File.Exists(libFullPath))
{
continue;
}

using (var input = assembly.GetManifestResourceStream(rsc))
using (var output = File.Create(libFullPath))
{
input?.CopyTo(output);
}

LogDebug($"[TVCore] Writing resrouce to disk: {libFilename}");
}

var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes()).Where(p => typeof(IService).IsAssignableFrom(p) && p.IsClass);

foreach (var type in types)
Expand Down Expand Up @@ -198,7 +260,7 @@ public static void ChangeChannel(bool direction)
{
if (CurrentChannelIndex == 0)
{
SetChannel((uint)ChannelIndexList.Count);
SetChannel((uint)ChannelIndexList.Count - 1);
}
else
{
Expand Down Expand Up @@ -253,6 +315,18 @@ public static void AddFavoriteChannels(IEnumerable<string> channelIds)
FavoritesSave();
}

public static void RemoveFavoriteChannel(string channelId)
{
LogDebug($"[TVCore] RemoveFavoriteChannel({channelId})");

if (ChannelFavorites.Contains(channelId))
{
ChannelFavorites.Remove(channelId);
}

FavoritesSave();
}

public static async Task<string> DownloadStringAndCache(string contentUri, string cacheFilename, int cacheTime)
{
LogDebug($"[TVCore] DownloadStringAndCache(url, {cacheFilename}, {cacheTime}) called");
Expand Down Expand Up @@ -403,9 +477,12 @@ private static void BlacklistSave()

private static void LogStart()
{
_logWriter.WriteLine(string.Empty);
_logWriter.WriteLine(string.Empty);
_logWriter.Flush();
lock (_logWriterLock)
{
_logWriter.WriteLine(string.Empty);
_logWriter.WriteLine(string.Empty);
_logWriter.Flush();
}
}

private static void Log(TvCoreLogLevel logLevel, string message)
Expand All @@ -415,12 +492,15 @@ private static void Log(TvCoreLogLevel logLevel, string message)
return;
}

var logLine = $"[{DateTime.UtcNow:O}]-[{logLevel.ToString().ToUpper().PadLeft(7)}]: {message}";
lock (_logWriterLock)
{
var logLine = $"[{DateTime.UtcNow:O}]-[{logLevel.ToString().ToUpper().PadLeft(7)}]: {message}";

_logWriter.WriteLine(logLine);
_logWriter.Flush();
_logWriter.WriteLine(logLine);
_logWriter.Flush();

_logBuffer.Enqueue(logLine);
_logBuffer.Enqueue(logLine);
}
}

private static void ChangeState(TvCoreState newState)
Expand Down Expand Up @@ -506,11 +586,6 @@ private static void CoreTimerOnElapsed(object sender, ElapsedEventArgs e)

ProgrammeChanged?.Invoke(newProgramme);
}

private static void HandleAppException(Exception exception)
{

}
}

public enum TvCoreState
Expand Down
5 changes: 5 additions & 0 deletions FoxIPTV/Controls/GuideLayoutPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ private void GenerateGuide(bool drawAnyway = false)

channelIndex += (uint)rowIdx - 1;

if (channelIndex >= TvCore.Channels.Count)
{
channelIndex -= (uint)TvCore.Channels.Count;
}

var columnTime = _timeCursorOn ? _timeCursor.ToUniversalTime() : DateTime.UtcNow;

var channel = TvCore.Channels[(int)channelIndex];
Expand Down
Loading

0 comments on commit a0b081b

Please sign in to comment.