aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Protobuf/Compilers/CCompiler.cs
blob: 91f8d150259d1bba2c198b7e15c60aa950863ca2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Helpers;
using Tango.Core.IO;

namespace Tango.Protobuf.Compilers
{
    /// <summary>
    /// Represents a protobuf C Compiler.
    /// </summary>
    /// <seealso cref="Tango.Protobuf.ProtoCompiler" />
    public class CCompiler : ProtoCompiler
    {
        /// <summary>
        /// Gets the compiler language.
        /// </summary>
        public override CompilerLanguage Language => CompilerLanguage.C;

        /// <summary>
        /// Gets the protobuf compiler CLI arguments (without input/output files!).
        /// </summary>
        /// <returns></returns>
        protected override string GetProtoArguments()
        {
            return "--c_out";
        }

        /// <summary>
        /// Gets the protobuf compiler CLI file name (override when using a compiler other than the default 'protoc.exe').
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        /// The compiler program must be located in the compilers folder.
        /// </remarks>
        protected override string GetProtoCompilerName()
        {
            return "protoc-c";
        }

        /// <summary>
        /// Compiles all files in the specified folder recursively.
        /// </summary>
        /// <param name="sourceFolder">The source folder</param>
        /// <returns>
        /// Compilation result.
        /// </returns>
        public override CompilerFolderResult CompileFolder(string sourceFolder, params String[] includeFolders)
        {
            var temp = TemporaryManager.Default.CreateFolder();
            PathHelper.CopyDirectory(sourceFolder, temp.Path, true);

            List<String> directories = Directory.GetDirectories(sourceFolder, "*", SearchOption.AllDirectories).Where(x => includeFolders == null || includeFolders.Length == 0 || includeFolders.Contains(Path.GetFileName(x))).ToList();

            foreach (var dir in Directory.GetDirectories(temp.Path))
            {
                if (!directories.Select(x => Path.GetFileName(x)).Contains(Path.GetFileName(dir)))
                {
                    Directory.Delete(dir, true);
                }
            }

            foreach (var file in Directory.GetFiles(temp.Path, "*.proto", SearchOption.AllDirectories))
            {
                String str = File.ReadAllText(file);

                var lines = str.ToLines();

                lines.RemoveAll(x => x.Contains("package "));

                for (int i = 0; i < lines.Count; i++)
                {
                    if (!lines[i].Contains("import ") || lines[i].StartsWith("//"))
                    {
                        string[] wordsAndComments = lines[i].Split('/');

                        string lineWords = wordsAndComments.FirstOrDefault();

                        string[] words = lineWords.Split(' ');

                        for (int j = 0; j < words.Length; j++)
                        {
                            words[j] = words[j].Split('.').LastOrDefault();
                        }

                        lines[i] = String.Join(" ", words);
                    }
                }

                str = String.Join(Environment.NewLine, lines);
                File.WriteAllText(file, str);
            }

            return base.CompileFolder(temp.Path);
        }
    }
}
class="n">DependencyProperty.Register("SelectedItems", typeof(IList), typeof(TouchListBox), new PropertyMetadata(null)); public IList ItemsSource { get { return (IList)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } } public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IList), typeof(TouchListBox), new PropertyMetadata(null, (d, e) => (d as TouchListBox).OnItemsSourceChanged())); public DataTemplate ItemTemplate { get { return (DataTemplate)GetValue(ItemTemplateProperty); } set { SetValue(ItemTemplateProperty, value); } } public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(TouchListBox), new PropertyMetadata(null)); public Style ItemContainerStyle { get { return (Style)GetValue(ItemContainerStyleProperty); } set { SetValue(ItemContainerStyleProperty, value); } } public static readonly DependencyProperty ItemContainerStyleProperty = DependencyProperty.Register("ItemContainerStyle", typeof(Style), typeof(TouchListBox), new PropertyMetadata(null)); public SelectionMode SelectionMode { get { return (SelectionMode)GetValue(SelectionModeProperty); } set { SetValue(SelectionModeProperty, value); } } public static readonly DependencyProperty SelectionModeProperty = DependencyProperty.Register("SelectionMode", typeof(SelectionMode), typeof(TouchListBox), new PropertyMetadata(SelectionMode.Single)); public bool IsMultiSelecting { get { return (bool)GetValue(IsMultiSelectingProperty); } set { SetValue(IsMultiSelectingProperty, value); } } public static readonly DependencyProperty IsMultiSelectingProperty = DependencyProperty.Register("IsMultiSelecting", typeof(bool), typeof(TouchListBox), new PropertyMetadata(false)); public LightTouchScrollViewer ScrollViewer { get { return (LightTouchScrollViewer)GetValue(ScrollViewerProperty); } set { SetValue(ScrollViewerProperty, value); } } public static readonly DependencyProperty ScrollViewerProperty = DependencyProperty.Register("ScrollViewer", typeof(LightTouchScrollViewer), typeof(TouchListBox), new PropertyMetadata(null)); public ICommand ItemSelectedCommand { get { return (ICommand)GetValue(ItemSelectedCommandProperty); } set { SetValue(ItemSelectedCommandProperty, value); } } public static readonly DependencyProperty ItemSelectedCommandProperty = DependencyProperty.Register("ItemSelectedCommand", typeof(ICommand), typeof(TouchListBox), new PropertyMetadata(null)); public RelayCommand<TouchListBoxItem> ListBoxItemLoadedCommand { get { return (RelayCommand<TouchListBoxItem>)GetValue(ListBoxItemLoadedCommandProperty); } set { SetValue(ListBoxItemLoadedCommandProperty, value); } } public static readonly DependencyProperty ListBoxItemLoadedCommandProperty = DependencyProperty.Register("ListBoxItemLoadedCommand", typeof(RelayCommand<TouchListBoxItem>), typeof(TouchListBox), new PropertyMetadata(null)); public bool DisableScrolling { get { return (bool)GetValue(DisableScrollingProperty); } set { SetValue(DisableScrollingProperty, value); } } public static readonly DependencyProperty DisableScrollingProperty = DependencyProperty.Register("DisableScrolling", typeof(bool), typeof(TouchListBox), new PropertyMetadata(false)); public bool DisableRipple { get { return (bool)GetValue(DisableRippleProperty); } set { SetValue(DisableRippleProperty, value); } } public static readonly DependencyProperty DisableRippleProperty = DependencyProperty.Register("DisableRipple", typeof(bool), typeof(TouchListBox), new PropertyMetadata(false)); public Visibility ScrollBarVisibility { get { return (Visibility)GetValue(ScrollBarVisibilityProperty); } set { SetValue(ScrollBarVisibilityProperty, value); } } public static readonly DependencyProperty ScrollBarVisibilityProperty = DependencyProperty.Register("ScrollBarVisibility", typeof(Visibility), typeof(TouchListBox), new PropertyMetadata(Visibility.Visible)); public String ValuePath { get { return (String)GetValue(ValuePathProperty); } set { SetValue(ValuePathProperty, value); } } public static readonly DependencyProperty ValuePathProperty = DependencyProperty.Register("ValuePath", typeof(String), typeof(TouchListBox), new PropertyMetadata(null)); public TouchListBox() { _awaitingSelectionItems = new List<TouchListBoxItem>(); Loaded += TouchListBox_Loaded; ListBoxItemLoadedCommand = new RelayCommand<TouchListBoxItem>(RegisterListBoxItemEvents); } private void RegisterListBoxItemEvents(TouchListBoxItem item) { if (item.Tag == null) { item.Tag = 1; item.RegisterForPreviewMouseOrTouchDown(OnItemMouseDown); item.RegisterForPreviewMouseOrTouchUp(OnItemMouseUp); } } private void TouchListBox_Loaded(object sender, RoutedEventArgs e) { if (!_isLoaded) { var stack = _items_control.FindChild<StackPanel>(); if (stack != null) { _items_control.Bind(ItemsControl.HeightProperty, stack, StackPanel.ActualHeightProperty, BindingMode.OneWay); _isLoaded = true; RegisterItemsEvents(); OnSelectedItemChanged(); } } } public override void OnApplyTemplate() { base.OnApplyTemplate(); _items_control = GetTemplateChild("PART_ItemsControl") as ItemsControl; ScrollViewer = GetTemplateChild("PART_ScrollViewer") as LightTouchScrollViewer; _items_control.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged; OnSelectedItemChanged(); } private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e) { if (_items_control.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated) { try { _items_control.UpdateLayout(); } catch { } RegisterItemsEvents(); OnSelectedItemChanged(); } } private void UpdateSelectedItems() { var selectedItem = GetItems().FirstOrDefault(x => x.IsSelected); if (selectedItem != null) { SelectedItem = selectedItem.DataContext; } if (SelectedItems != null) { SelectedItems.Clear(); foreach (var item in GetItems().Where(x => x.IsSelected)) { SelectedItems.Add(item.DataContext); } } } private List<TouchListBoxItem> GetItems() { return _items_control.FindVisualChildren<TouchListBoxItem>().ToList(); } private void RegisterItemsEvents() { var items = GetItems(); foreach (var item in items) { if (item.Tag == null) { item.Tag = 1; item.RegisterForPreviewMouseOrTouchDown(OnItemMouseDown); item.RegisterForPreviewMouseOrTouchUp(OnItemMouseUp); } } } private async void OnItemMouseDown(object sender, MouseOrTouchEventArgs e) { var item = (e.Source is TouchListBoxItem) ? e.Source as TouchListBoxItem : (e.Source as DependencyObject).FindAncestor<TouchListBoxItem>(); var otherItems = GetItems().Where(x => x != item).ToList(); if (SelectionMode == SelectionMode.Multiple) { if (!IsMultiSelecting) { _awaitingSelectionItems.Add(item); await Task.Delay(500); if (_awaitingSelectionItems.Contains(item)) { _awaitingSelectionItems.Remove(item); if (ScrollViewer.IsMouseTouchDown && !ScrollViewer.IsScrolling) { _firstMultiSelectionItem = item; otherItems.ForEach(x => x.IsSelected = false); item.IsSelected = true; IsMultiSelecting = true; UpdateSelectedItems(); } } } } } private void OnItemMouseUp(object sender, MouseOrTouchEventArgs e) { if (ScrollViewer.IsAfterScrolling) return; var item = (e.Source is TouchListBoxItem) ? e.Source as TouchListBoxItem : (e.Source as DependencyObject).FindAncestor<TouchListBoxItem>(); var otherItems = GetItems().Where(x => x != item).ToList(); if (!IsMultiSelecting) { _awaitingSelectionItems.Remove(item); if (SelectionMode != SelectionMode.Multiple) { otherItems.ForEach(x => x.IsSelected = false); item.IsSelected = true; ItemSelectedCommand?.Execute(item.DataContext); } else { ItemSelectedCommand?.Execute(item.DataContext); } } else if (SelectionMode == SelectionMode.Multiple && IsMultiSelecting) { if (!ScrollViewer.IsScrolling) { if (item != _firstMultiSelectionItem) { item.IsSelected = !item.IsSelected; } _firstMultiSelectionItem = null; if (!item.IsSelected && otherItems.All(x => !x.IsSelected)) { IsMultiSelecting = false; } } } UpdateSelectedItems(); } private void OnSelectedItemChanged() { var items = GetItems(); items.ForEach(x => x.IsSelected = false); TouchListBoxItem item = null; if (String.IsNullOrWhiteSpace(ValuePath) || SelectedItem == null) { item = items.FirstOrDefault(x => x.DataContext.Equals(SelectedItem)); } else { item = items.FirstOrDefault(x => x.DataContext.GetPropertyValueByPath(ValuePath).Equals(SelectedItem.GetPropertyValueByPath(ValuePath))); } if (item != null) { item.IsSelected = true; UpdateSelectedItems(); } } private void OnItemsSourceChanged() { if (ScrollViewer != null) { ScrollViewer.ScrollToTop(); } } public void ScrollToItem(Object item) { var ee = ScrollViewer.FindVisualChildren<FrameworkElement>(); var element = ScrollViewer.FindVisualChildren<FrameworkElement>().FirstOrDefault(x => x.DataContext == item); ScrollViewer.ScrollToElement(element); } } }